title: Authentication error between c# client and java server
body: <p>I have created a java server with self-signed certificate and I can connect to it with openssl and communicate with it. But the c# client I wrote keeps getting a <code>System.InvalidOperationException</code> (This operation can only be used with authenticated environment (mirror translated to english)) during <code>WriteAsync</code>.</p>
  
  <p>The server says: "javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshake".</p>
  
  <p>c# client:</p>
  
  <pre><code>TcpClient tcpClient = new TcpClient("127.0.0.1", 40);
  using (SslStream sslStream = new SslStream(tcpClient.GetStream(), false, new RemoteCertificateValidationCallback(this.ValidateServerCertificate))) {
      sslStream.AuthenticateAsClientAsync("127.0.0.1");
  
      byte[] request = Encoding.ASCII.GetBytes(prepareRequest(email, password));
      sslStream.WriteAsync(request, 0, request.Length); //this is where I get exception
  
  }
  tcpClient.Close();
  </code></pre>
  
  <p>The server does not require client certificate. What should I do to make it happen?</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>Here is the openssl output when I connect with <code>openssl s_client -connect 127.0.0.1:40</code>:</p>
  
  <pre><code>CONNECTED(00000003)
  depth=0 C = .., ST = .., L = .., O = Unknown, OU = Unknown, CN = ..
  verify error:num=18:self signed certificate
  verify return:1
  depth=0 C = .., ST = .., L = .., O = Unknown, OU = Unknown, CN = ..
  verify return:1
  ---
  Certificate chain
   0 s:/C=../ST=../L=../O=Unknown/OU=Unknown/CN=..
     i:/C=../ST=../L=../O=Unknown/OU=Unknown/CN=..
  ---
  Server certificate
  -----BEGIN CERTIFICATE-----
  MIIDcTCCAlmgAwIBAgIEKrrdADANBgkqhkiG9w0BAQsFADBpMQswCQYDVQQGEwJI
  VTENMAsGA1UECBMEUGVzdDERMA8GA1UEBxMIQnVkYXBlc3QxEDAOBgNVBAoTB1Vu
  a25vd24xEDAOBgNVBAsTB1Vua25vd24xFDASBgNVBAMTC0RhdmlkIE9yY3NrMB4X
  DTE3MDgxNDEyMDU1MFoXDTE4MDgwOTEyMDU1MFowaTELMAkGA1UEBhMCSFUxDTAL
  BgNVBAgTBFBlc3QxETAPBgNVBAcTCEJ1ZGFwZXN0MRAwDgYDVQQKEwdVbmtub3du
  MRAwDgYDVQQLEwdVbmtub3duMRQwEgYDVQQDEwtEYXZpZCBPcmNzazCCASIwDQYJ
  KoZIhvcNAQEBBQADggEPADCCAQoCggEBAJToS08AGHt74oSP2jL1rkfnTlcBBKXZ
  nrJCmn/S4LHATh9xwsvM8dHuN+CPR6ZJAhLUPnFW5jP8DNExsUShvEFXSKZUw43W
  VDQNctvuZ4KAdgh878QQv94XN4k0pZkohrReCmLNU6cDOsL8Mv75gSmjWqN/SUml
  CGCKADhgeS9rUwHeIqEfxFjLdnDHq1vmdmGDgtO/VQnTv2KG8xkUtRIOn/sTPtc/
  dFcmTN7wIaq5q7I6Ujrp2oTVdDdlL6xODy2TGuRoD+1cbFhWqNgHbZzKdeChGDrl
  73+znrJnbqkRvPzb5zPzVQUAHILFvAAN5/ef8D1ss0BkhF71ySt7bzUCAwEAAaMh
  MB8wHQYDVR0OBBYEFE0ApKA3xeiPt1qcwXBSmKPxi+Y1MA0GCSqGSIb3DQEBCwUA
  A4IBAQACkxJOcxnacXIPugZY9aL0cfgV3FKCWs3ZxKfMksQ2v9xLl8Z0QC5DBhoT
  zAJ5f9eE9f/DRIaqjzL9nbD/3sx3W6CFdXPYsX6jWyWbvzzT+8tGEmof4Dmi3RAr
  98e7wFFPYYIMMptea/MmeF/pM2O0EVAc3W+HTXdEg/DFYHQOgFBlQUYWAcAQfXCX
  uaza/3uQYAjBLGRvVyR2akwLSulxee7wdbQX5TfiDMeL10DGuaelAUQswhOzxaiZ
  X3bKlcGxoS5QLHIDVHHaFxya3MFoejrGxXLn0SyaVcJ/DVDByrWjeu7zf964vw86
  hHMOzi4KwRlBrUn9oz2LAPIYmBIs
  -----END CERTIFICATE-----
  subject=/C=../ST=../L=../O=Unknown/OU=Unknown/CN=..
  issuer=/C=../ST=../L=../O=Unknown/OU=Unknown/CN=..
  ---
  No client certificate CA names sent
  Peer signing digest: SHA512
  Server Temp Key: ECDH, P-256, 256 bits
  ---
  SSL handshake has read 1369 bytes and written 431 bytes
  ---
  New, TLSv1/SSLv3, Cipher is ECDHE-RSA-AES128-GCM-SHA256
  Server public key is 2048 bit
  Secure Renegotiation IS supported
  Compression: NONE
  Expansion: NONE
  No ALPN negotiated
  SSL-Session:
      Protocol  : TLSv1.2
      Cipher    : ECDHE-RSA-AES128-GCM-SHA256
      Session-ID: 5995A92B659904ED327561C58121A90D1C3D1B538F190E32E552D575DACB7C9E
      Session-ID-ctx:
      Master-Key: F0190CF037F957E64A2B8E307D4FDC71EFF81881A091677D0DA6790B8100601330D50086F12AB330EE264B49D0053D84
      Key-Arg   : None
      PSK identity: None
      PSK identity hint: None
      SRP username: None
      Start Time: 1502980395
      Timeout   : 300 (sec)
      Verify return code: 18 (self signed certificate)
  ---
  Message from server...
  </code></pre>