title: Socket and Authentication failed because the remote party has closed the transport stream exception in WPF
body: <p>I am trying to connect with Java netty based server, which auto generates certificates for itself (and server guys told me, that is accepting any certificate from client side for now).</p>
  
  <p>My task was to migrate TcpSocket connection into Tls encrypted connection.</p>
  
  <p>First of all, I converted TcpSocket into NetworkStream:</p>
  
  <pre><code>using (var client = new NetworkStream(connection.TcpSocket))
  {
      if (client.CanRead)
      {
          client.BeginRead(recvState.DataBuffer, 0, recvState.DataBuffer.Length, ReceiveCallback,
          recvState);
      }
  }
  </code></pre>
  
  <p>and that is working perfectly. So then, I decided to build SslAuthentication - like here:</p>
  
  <pre><code>using (var client = new NetworkStream(connection.TcpSocket))
  using (var sslStream = new SslStream(client, false, App_CertificateValidation))
  {
      var clientCertificate = new X509Certificate2("client.pfx");
      var clientCertificateCollection = new X509Certificate2Collection(new[] { clientCertificate });
      sslStream.AuthenticateAsClient("MyServer", clientCertificateCollection, SslProtocols.Tls, false);
  
      if (sslStream.CanRead)
      {
          sslStream.BeginRead(recvState.DataBuffer, 0, recvState.DataBuffer.Length, ReceiveCallback,
          recvState);
      }
  }
  </code></pre>
  
  <p>Where <code>client.pfx</code> is random certificate with no password, as a file in project and also imported into <code>Current User Certificates &gt; Personal &gt; Certificates</code> in <code>certmgr.msc</code>.</p>
  
  <p>The problem is AuthenticateAsClient throws an </p>
  
  <blockquote>
    <p>System.IO.IOException: Authentication failed because the remote party
    has closed the transport stream exception.</p>
  </blockquote>
  
  <p>Also, if the hostname in AuthenticateAsCtlient method means anything, if server accepts every certificate? Should I put there something significant?</p>
  
  <p>I still can contact with the server guys, so I can ask them about everything - do we need any additional information?</p>