<commit_before>using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace Rhino.Licensing
{
	public class SntpClient
	{
		private const byte SntpDataLength = 48;
		private readonly string[] hosts;
		private int index = -1;

		public SntpClient(string[] hosts)
		{
			this.hosts = hosts;
		}

		private static bool GetIsServerMode(byte[] sntpData)
		{
			return (sntpData[0] & 0x7) == 4 /* server mode */;
		}

		private static DateTime GetTransmitTimestamp(byte[] sntpData)
		{
			var milliSeconds = GetMilliSeconds(sntpData, 40);
			return ComputeDate(milliSeconds);
		}

		private static DateTime ComputeDate(ulong milliseconds)
		{
			return new DateTime(1900, 1, 1).Add(TimeSpan.FromMilliseconds(milliseconds));
		}

		private static ulong GetMilliSeconds(byte[] sntpData, byte offset)
		{
			ulong intpart = 0, fractpart = 0;

			for (var i = 0; i <= 3; i++)
			{
				intpart = 256*intpart + sntpData[offset + i];
			}
			for (var i = 4; i <= 7; i++)
			{
				fractpart = 256*fractpart + sntpData[offset + i];
			}
			var milliseconds = intpart*1000 + (fractpart*1000)/0x100000000L;
			return milliseconds;
		}

		public void BeginGetDate(Action<DateTime> getTime, Action failure)
		{
			index += 1;
			if (hosts.Length <= index)
			{
				failure();
				return;
			}
			try
			{
				var host = hosts[index];
				Dns.BeginGetHostAddresses(host, EndGetHostAddress, 
                    new State(null, null, getTime, failure) );
			}
			catch (Exception)
			{
				// retry, recursion stops at the end of the hosts
				BeginGetDate(getTime, failure);
			}
		}

	    private void EndGetHostAddress(IAsyncResult asyncResult)
        {
	        var state = (State) asyncResult.AsyncState;
	        try
	        {
                Thread.Sleep(1000);
	            var addresses = Dns.EndGetHostAddresses(asyncResult);
	            var endPoint = new IPEndPoint(addresses[0], 123);

	            var socket = new UdpClient();
	            socket.Connect(endPoint);
	            socket.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 500);
	            socket.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, 500);
	            var sntpData = new byte[SntpDataLength];
	            sntpData[0] = 0x1B; // version = 4 & mode = 3 (client)

                socket.BeginSend(sntpData, sntpData.Length, EndSend, new State(socket, endPoint, state.GetTime, state.Failure));
	        }
	        catch (Exception)
	        {
                // retry, recursion stops at the end of the hosts
                BeginGetDate(state.GetTime, state.Failure);
	
	        }<commit_msg>Thread.Sleep should not existing in production code!!!<commit_after>using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace Rhino.Licensing
{
	public class SntpClient
	{
		private const byte SntpDataLength = 48;
		private readonly string[] hosts;
		private int index = -1;

		public SntpClient(string[] hosts)
		{
			this.hosts = hosts;
		}

		private static bool GetIsServerMode(byte[] sntpData)
		{
			return (sntpData[0] & 0x7) == 4 /* server mode */;
		}

		private static DateTime GetTransmitTimestamp(byte[] sntpData)
		{
			var milliSeconds = GetMilliSeconds(sntpData, 40);
			return ComputeDate(milliSeconds);
		}

		private static DateTime ComputeDate(ulong milliseconds)
		{
			return new DateTime(1900, 1, 1).Add(TimeSpan.FromMilliseconds(milliseconds));
		}

		private static ulong GetMilliSeconds(byte[] sntpData, byte offset)
		{
			ulong intpart = 0, fractpart = 0;

			for (var i = 0; i <= 3; i++)
			{
				intpart = 256*intpart + sntpData[offset + i];
			}
			for (var i = 4; i <= 7; i++)
			{
				fractpart = 256*fractpart + sntpData[offset + i];
			}
			var milliseconds = intpart*1000 + (fractpart*1000)/0x100000000L;
			return milliseconds;
		}

		public void BeginGetDate(Action<DateTime> getTime, Action failure)
		{
			index += 1;
			if (hosts.Length <= index)
			{
				failure();
				return;
			}
			try
			{
				var host = hosts[index];
				Dns.BeginGetHostAddresses(host, EndGetHostAddress, 
                    new State(null, null, getTime, failure) );
			}
			catch (Exception)
			{
				// retry, recursion stops at the end of the hosts
				BeginGetDate(getTime, failure);
			}
		}

	    private void EndGetHostAddress(IAsyncResult asyncResult)
        {
	        var state = (State) asyncResult.AsyncState;
	        try
	        {
	            var addresses = Dns.EndGetHostAddresses(asyncResult);
	            var endPoint = new IPEndPoint(addresses[0], 123);

	            var socket = new UdpClient();
	            socket.Connect(endPoint);
	            socket.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 500);
	            socket.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, 500);
	            var sntpData = new byte[SntpDataLength];
	            sntpData[0] = 0x1B; // version = 4 & mode = 3 (client)

                socket.BeginSend(sntpData, sntpData.Length, EndSend, new State(socket, endPoint, state.GetTime, state.Failure));
	        }
	        catch (Exception)
	        {
                // retry, recursion stops at the end of the hosts
                BeginGetDate(state.GetTime, state.Failure);
	
	        }
{'fraction_non_alphanumeric': 0.1044523764794289, 'fraction_numerical': 0.023670862295697915, 'mean_word_length': 2.6666666666666665, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5691141', 'n_tokens_mistral': 2018, 'n_tokens_neox': 1642, 'n_words': 407}