title: Certificate error: Authentication failed because the remote party has closed the transport stream
body: <p>I am making proxy server. I used <a href="http://www.codeproject.com/Articles/93301/Implementing-a-Multithreaded-HTTP-HTTPS-Debugging" rel="nofollow">Implementing a Multithreaded HTTP/HTTPS Debugging Proxy Server in C#</a> as a reference.</p>
  
  <p>The piece of code:</p>
  
  <pre><code>    private void ProccessRequest(Socket client)
      {
          // Create network stream from socket for read and write data
          Stream networkStream = new NetworkStream(client, true);
          // Create stream reader for read data from socket
          StreamReader clientReader = new StreamReader(networkStream);
          // For read and write data to socket
          // Same for http and https requests
          Stream clientStream = networkStream;            
  
          try
          {
              if (client.Connected)
              {
                  // Get first request line
                  string str = clientReader.ReadLine();
                  if (!String.IsNullOrEmpty(str))
                  {
                      string[] tmpStr = str.Split(SapceSplit, 3);
  
                      String method = tmpStr[0];
                      String remoteUrl = tmpStr[1];
  
                      if (method.Equals("CONNECT", StringComparison.OrdinalIgnoreCase))
                      {
                          remoteUrl = "https://" + remoteUrl;
  
                          // Read all information from socket
                          while (!String.IsNullOrEmpty(clientReader.ReadLine())) ;
  
                          // Tell browser that connection established
                          StreamWriter connectWriter = new StreamWriter(networkStream);
                          connectWriter.WriteLine("HTTP/1.0 200 Connection established");
                          connectWriter.WriteLine(String.Format("Timestamp: {0}", DateTime.Now.ToString()));
                          connectWriter.WriteLine();
                          connectWriter.Flush();
  
                          // Create SSL stream
                          SslStream sslStream = new SslStream(networkStream, false);
                          // Set sertificate for decrypt the client's traffic
                          sslStream.AuthenticateAsServer(_certificate, false, 
                              SslProtocols.Tls | SslProtocols.Ssl3 | SslProtocols.Ssl2, true);
  
                          // HTTPS server created - we can now decrypt the client's traffic
                          clientReader = new StreamReader(sslStream);
                          clientStream = sslStream;
                          // read the new http command.
                          str = clientReader.ReadLine();
                          tmpStr = str.Split(SapceSplit, 3);
                          method = tmpStr[0].ToUpper();
                          remoteUrl = remoteUrl + tmpStr[1];
                      }
  </code></pre>
  
  <p>But on this line:</p>
  
  <pre><code>sslStream.AuthenticateAsServer(_certificate, false, 
          SslProtocols.Tls | SslProtocols.Ssl3 | SslProtocols.Ssl2, true);
  </code></pre>
  
  <p>I am getting the error: Authentication failed because the remote party has closed the transport stream.</p>
  
  <p>I am loading certificate with private key:</p>
  
  <pre><code>_certificate = new X509Certificate2("D:\\cert2.pfx", "1234", X509KeyStorageFlags.MachineKeySet);
  </code></pre>