title: Detecting unexpected socket disconnect
body: <p>This is not a question about how to do this, but a question about whether it's wrong what I'm doing. I've read that it's not possible to detect if a socket is closed unexpectedly (like killing the server/client process, pulling the network cable) while waiting for data (BeginReceive), without use of timers or regular sent messages, etc. But for quite a while I've been using the following setup to do this, and so far it has always worked perfectly.</p>
  
  <pre><code>public void OnReceive(IAsyncResult result)
  {
      try
      {
          var bytesReceived = this.Socket.EndReceive(result);
  
          if (bytesReceived &lt;= 0)
          {
              // normal disconnect
              return;
          }
  
          // ...
  
          this.Socket.BeginReceive...;
      }
      catch // SocketException
      {
          // abnormal disconnect
      }
  }
  </code></pre>
  
  <p>Now, since I've read it's not easily possible, I'm wondering if there's something wrong with my method. Is there? Or is there a difference between killing processes and pulling cables and similar?</p>