title: Self-healing SslStream
body: <p>I'm writing a service that needs to maintain a long running SSL connection to a remote server. I need this server to be self-healing, that is if it's disconnected for any reason then the next time it's written to it will reconnect. I've tried this:</p>
  
  <pre><code>bool isConnected = client.Connected &amp;&amp; client.Client.Poll(0, SelectMode.SelectWrite) &amp;&amp; stream.CanWrite;
  
  if (!isConnected )
  {
      this.connected = false;
      GetConnection();
  }
  
  stream.Write(bytes, 0, bytes.Length);
  stream.Flush();
  </code></pre>
  
  <p>But I find it doesn't act as I would expect it. If I simulate a network outage by disabling my wifi, I'm still able to write to the stream with stream.Write() for approximately 20 seconds. Then next time I try to write to it, none of client.Connected, client.Client.Poll(), or stream.CanWrite() return false, but when I go to write to the stream I get a socket exception. Finally, if I try to recreate the connection, I get this exception: An existing connection was forcibly closed by the remote host.</p>
  
  <p>I would appreciate any help create a long running SslStream that can withstand network failure. Thanks!</p>