(define clock:sync-adjust
(lambda (server-proc local-proc samples wait-time . args)
(let loop ((i samples)
(lst '()))
(sys:sleep wait-time)
(if (> i 0)
(loop (- i 1)
(cons (clock:offset-from-server server-proc local-proc) lst))
(let* ((l1 (cddr (cl:sort lst <))) ;; remove bottom 2
(l2 (cddr (reverse l1)))) ;; remote top 2
;; check tolerance of return
(if (> (math:std-deviation l2)
(if (null? args) 0.0005 (car args)))
(begin (log-info "clock sync outside tolerance - making no time adjustment: " (math:std-deviation l2))
(log-info "clock times: " lst))
(begin (log-info 'clock 'successfully 'adjusted 'by (/ (apply + l2) (length l2)) 'seconds)
(clock:adjust-offset (/ (apply + l2) (length l2))))))))))