title: Determine if SocketException from UdpClient.Receive was caused by a timeout
body: <p>I have a <code>UdpClient</code> receiving data with a timeout set, e.g.:</p>
  
  <pre><code>UdpClient client = new UdpClient(new IPEndPoint(IPAddress.Any, 12345));
  client.Client.ReceiveTimeout = 5000;     // 5 second timeout
  
  while (!shutdown) {
      IPEndPoint source = null;
      byte[] data = client.Receive(ref source);  
  }
  </code></pre>
  
  <p><code>Receive</code> is <a href="https://docs.microsoft.com/en-us/dotnet/api/system.net.sockets.udpclient.receive?view=netframework-4.8#System_Net_Sockets_UdpClient_Receive_System_Net_IPEndPoint__" rel="nofollow noreferrer">documented</a> as throwing a <code>SocketException</code> if "an error occurred when accessing the socket". The behavior of setting <code>ReceiveTimeout</code> is also <a href="https://docs.microsoft.com/en-us/dotnet/api/system.net.sockets.socket.receivetimeout?view=netframework-4.8" rel="nofollow noreferrer">documented</a> as throwing a <code>SocketException</code> if the synchronous operation times out.</p>
  
  <p>If I get a <code>SocketException</code> from <code>Receive</code> when a timeout is set, how can I determine if the exception was caused by a timeout rather than a more serious error?</p>
  
  <p>I did confirm that the exception thrown on timeout is an actual <code>SocketException</code> and not some specific subclass of it. The only thing I can really think of is checking the exception message, which is a hack that I'm not really excited about.</p>