title: Correct way to detect a client has disconnected from TCP/IP
body: <p>I have used a Asynchronous TCP/IP server, everything works fine but when a client disconnects due to error or forced exit of the application it also closes my server due to an exception of type IO.IOException. The exception occurs in the following sub:</p>
  
  <pre><code>   Private Sub ReadCallback(ByVal result As IAsyncResult)
          Try
  
  
              Dim client As Client = TryCast(result.AsyncState, Client)
              If client Is Nothing Then
                  Return
              End If
              'MsgBox(client.ClientID)
              Dim networkStream As NetworkStream = client.NetworkStream
              Dim read As Integer = networkStream.EndRead(result) **' ERRORS HERE!**
              If read = 0 Then
              Dim client As Client = TryCast(result.AsyncState, Client)
              SyncLock Me.Clients
                  Me.Clients.Remove(Client.ClientID)
                  Return
              End SyncLock
              End If
  </code></pre>
  
  <p>The below code throws an IO.IOException when a client disconnects from my TCP Server:</p>
  
  <pre><code>Dim read As Integer = networkStream.EndRead(result) **' ERRORS HERE!**
  </code></pre>
  
  <p>Other then catching the IO.IOException how can I prevent this from creating the exception in the first place. Basically I'd like to remove my client when this scenario occurs, at the moment as a work around I have removed the client on an IO.IOException and rethrown the method. Would rather a better method.</p>