private void SyncClock(int remoteTime)
{
//Calculate the offset between the remote and local time
m_clockOffset = remoteTime - Environment.TickCount;
//Calculate the average of any round-trip times we have
int averageRTT = m_roundTripTimes.Sum() / m_roundTripTimes.Count;
//Calculate the standard deviation of the RTTs
int stddev = (int)Math.Sqrt(m_roundTripTimes.Select(n => (n - averageRTT)*(n - averageRTT)).Sum() / m_roundTripTimes.Count);
//Calculate the median RTT
int median = m_roundTripTimes.OrderBy(i => i).ElementAt(m_roundTripTimes.Count / 2);
//Remove any RTTs that are > 1 standard deviation away from the median and calculate the average again
int unskewedAverage = m_roundTripTimes.Where(i => Math.Abs(i - median) < stddev).Sum() / m_roundTripTimes.Count;
//Apply half of this average to the clock offset to account for latency
m_clockOffset += unskewedAverage / 2;
}