title: Corrupted BufferedStream when reconnecting a dropped NetworkStream
body: <p>Heres the scenerio...</p>
  
  <p><strong>Server</strong></p>
  
  <ul>
  <li>I have <code>TcpListener</code> server that is accepting connections</li>
  <li>When a connection is made, I wrap the <code>TcpClient.GetStream()</code> in a buffered stream.</li>
  <li>Then I am using a <code>BinaryFormatter</code> to deserialize incoming data from the stream</li>
  </ul>
  
  <p><strong>Client</strong></p>
  
  <ul>
  <li>I connect to the server</li>
  <li>When connected, I wrap the <code>TcpClient.GetStream()</code> in a buffered stream.</li>
  <li>Then I use a <code>BinaryFormatter</code> to serialize data over the Stream.</li>
  </ul>
  
  <p>All works wells for as long as the connection stays active.  I've tested over periods of hours.</p>
  
  <p><strong>The Problem</strong></p>
  
  <ul>
  <li>Then I stop the Server to simulate a network fault (I'm using localhost)</li>
  <li>The client will detect the fault (exception thrown/handled)</li>
  <li>The client will re-connect later with a new <code>TcpClient</code> and new <code>BufferedStream</code></li>
  <li>The the client will use a new <code>BinaryFormatter</code> to start serializing
  data again.</li>
  </ul>
  
  <p>Now however, the server will get all kinds of strange deserialization Exceptions.
  I've gone as low as looking at the bytes of the Stream and to my surprise found that <strong><em>the stream is being corrupted</em></strong>!... but only after a re-connect scenerio.
  And, getting rid of the <code>BufferedStream</code> and using the <code>NetworkStream</code> directly fixes the issue.  You can re-connect many many times without seeing this issue.</p>
  
  <p>What is going on with the <code>BufferedStream</code>?  Every reconnect I create a new <code>BufferedStream</code>, but could the previous stream be affecting the new one some how?</p>
  
  <p>Has anyone seen anything like this?</p>
  
  <p>Unfortunatly I can't post the entire code, but here is an example of what I am doing...</p>
  
  <p><strong>Example Server Code</strong></p>
  
  <pre><code>while (true)
  {//Accept connections and spawn threads to handle incoming connections
       TcpClient connection = server.AcceptTcpClient();
  }
  
  //Done on another thread
  Stream stream = new BufferedStream(connection.GetStream());
  BinaryFormatter formatter = new BinaryFormatter();
  object data = formatter.Deserialize(stream);
  </code></pre>
  
  <p><strong>Example Client Code</strong></p>
  
  <pre><code>_connection = new TcpClient();
  _connection.Connect(_remoteAddress);
  NetworkStream netStream = _connection.GetStream();
  Stream stream = new BufferedStream(netStream);
  BinaryFormatter formatter = new BinaryFormatter();
  formatter.Serialize(stream, data);
  stream.Flush();
  </code></pre>
  
  <p>Without using BufferedStream everything works ok.  Also when reconnecting the above client proccess is repeated.</p>