title: Timeout in Socket.Recieve
body: <p>I filched the following code from net to get GMT time from the site time-a.nist.gov. This code runs great but once in a blue moon it hangs. When I pause debugging the green arrow with the message "This is the next statement to execute when the thread returns from the current location"  shows next to s.Receive(ntpData);         </p>
  
  <pre><code>        public static DateTime GetNetworkTime(IPEndPoint ep)
          {
              Socket s = new Socket(AddressFamily.InterNetwork, 
                                    SocketType.Dgram, ProtocolType.Udp);
  
              s.Connect(ep);
  
              byte[] ntpData = new byte[48]; // RFC 2030
              ntpData[0] = 0x1B;
              for (int i = 1; i &lt; 48; i++)
                  ntpData[i] = 0;
  
              s.Send(ntpData);  
              s.Receive(ntpData);
              .
              .
         }
  </code></pre>
  
  <p>So I added the lines </p>
  
  <pre><code> s.SendTimeout = 1000;
   s.ReceiveTimeout = 1000;
  </code></pre>
  
  <p>Now it doesnt hang but throws the exception "A connection attempt failed because the connected party did not respond properly after a period of time"</p>
  
  <p>StackTrace: at System.Net.Socket.Recieve(Byte[] buffer, Int32 offset,Int32, SocketFlags socketFlags)</p>
  
  <p>Is this a server site (time-a.nist.gov) issue and hence no recourse except to call it again?</p>
  
  <p>Also is there any way to handle this exception and call the GetNetworkTime method again.</p>
  
  <p>I'd appreciate any help on this problem.
  Thanks in advance</p>