title: TcpClient/NetworkStream not detecting disconnection
body: <p>I created a simple persistent socket connection for our game using <code>TcpClient</code> and <code>NetworkStream</code>. There's no problem connecting, sending messages, and disconnecting normally (quitting app/server shuts down/etc).</p>
  
  <p>However, I'm having some problems where, in certain cases, the client isn't detecting a disconnection from the server. The easiest way I have of testing this is to pull out the network cable on the wifi box, or set the phone to airplane mode, but it's happened in the middle of a game on what should otherwise be a stable wifi.</p>
  
  <p>Going through the docs for <code>NetworkStream</code> etc, it says that the only way to detect a disconnection is to try to write to the socket. Fair enough, except, when I try, the write passes as if nothing is wrong. I can write multiple messages like this, and everything seems fine. It's only when I plug the cable back in that it sees that it's disconnected (all messages are buffered?).</p>
  
  <p>The <code>TcpClient</code> is set to <code>NoDelay</code>, and there's a <code>Flush()</code> called after every write anyway.</p>
  
  <p>What I've tried:</p>
  
  <ul>
  <li>Writing a message to the <code>NetworkStream</code> - no joy</li>
  <li><code>CanWrite</code>, <code>Connected</code>, etc all return true</li>
  <li><code>TcpClient.Client.Poll( 1000, SelectMode.SelectWrite );</code> - returns true</li>
  <li><code>TcpClient.Client.Poll( 1000, SelectMode.SelectRead ) &amp;&amp; TcpClient.Client.Available == 0</code> - returns true</li>
  <li><code>TcpClient.Client.Receive(buffer, SocketFlags.Peek) == 0</code> - when connected, blocks for about 10-20s, then returns true. When no server, blocks forever(?)</li>
  <li><code>NetworkStream.Write()</code> - doesn't throw an error</li>
  <li><code>NetworkStream.BeginWrite()</code> - doesn't throw an error (not even when calling <code>EndWrite()</code>)</li>
  <li>Setting a <code>WriteTimeout</code> - had no effect</li>
  <li>Having a specific time where we haven't received a message from the server (normally there's a keep-alive) - I had this, but removed it, as we were getting a lot of false-positives due to lag etc (some clients would see between 10-20s of lag)</li>
  </ul>
  
  <p>So am I doing something wrong here? Is there any way to get the <code>NetworkStream</code> to throw an error (like it should) when writing to a socket that should be disconnected?</p>
  
  <p>I've no problem with a keep-alive (the default case is the server will notify the client that it hasn't received anything in a while, and the client will send a heartbeat), but at the minute, according to the <code>NetworkStream</code> everything's hunky-dory.</p>
  
  <p>It's for a game, so ideally the detection should be quick enough (as the user can still move through the game until they need to make a server call, some of which will block the UI, so the game seems broken).</p>
  
  <p>I'm using Unity, so it's .Net 2.0</p>