title: C# WebSocketServer wss:// connection fail
body: <p>I've written a WebSocket Server which currently works only with ws:// connections.<br>
  Tested with <a href="http://www.websocket.org/echo.html" rel="nofollow">http://www.websocket.org/echo.html</a> (on Chrome) and <a href="http://websocket4net.codeplex.com/" rel="nofollow">http://websocket4net.codeplex.com/</a> .</p>
  
  <p>Now I want to also support <code>wss://</code> connections (TLS).<br>
  I've got it to work with WebSocket4Net but when using the echo test,<br>
  javascript immediately throws an exception after <code>SslStream.AuthenticateAsServer();</code> is executed in C# code.</p>
  
  <p>It does its TLS negotiation, but Chrome rejects the connection.<br>
  SslStream Reads and writes (ServerSide done by AuthenticateAsServer):</p>
  
  <pre><code>Read(5, 0, 5) == 5
  Read(517, 5, 512) == 512
  Write(887, 0, 887)
  Read(887, 0, 5) == 5
  Read(887, 5, 262) == 262
  Read(5, 0, 5) == 5
  Read(6, 5, 1) == 1
  Read(5, 0, 5) == 5
  Read(53, 5, 48) == 48
  Write(59, 0, 59)
  </code></pre>
  
  <p>I used the following steps to add the certificate:</p>
  
  <pre><code>makecert -sv CA.pvk -r -n "CN=Dev" -a sha256 -len 2048 -sky signature -cy authority CA.cer
  makecert -ic CA.cer -iv CA.pvk -n "CN=localhost, CN=127.0.0.1" -a sha256 -len 2048 -sky exchange -sv CA_localhost.pvk -pe CA_localhost.cer
  cert2spc CA_localhost.cer CA_localhost.spc
  pvkimprt -pfx CA_localhost.spc CA_localhost.pvk //Select export
  //Import CA.cer into your Computer store's Trusted Root Certification Authorities (certmgr.msc)
  //Import the private key that the server is going to use into the server machine's Personal store.
  //This is achieved by importing the .pfx file that you generated earlier.
  </code></pre>
  
  <p>SslStream created by <code>new SslStream(new NetworkStream(clientSocket), true)</code><br>
  For AuthenticateAsServer I'm using the "CA_localhost.cer" certificate.</p>
  
  <p>After making sure the certificates are displayed in certmgr.msc I opened 
  <a href="http://www.websocket.org/echo.html" rel="nofollow">http://www.websocket.org/echo.html</a> in Chrome and used <code>wss://localhost:12345</code> as the address (for which my server is listening) and also checking the checkbox.<br>
  After clicking <code>Connect</code> I only get:</p>
  
  <pre><code> ERROR: undefined
   DISCONNECTED
  </code></pre>
  
  <p>The javascript console displaying:<br>
  WebSocket connection to 'wss://127.0.0.1:12345/?encoding=text' failed: Connection closed before receiving a handshake response </p>
  
  <p>On the serverside after <code>AuthenticateAsServer</code> the properties <code>IsAuthenticated</code> and <code>IsEncrypted</code> are set to true, but immediately after that I receive data of zero length, closing the connection.</p>
  
  <p>I'm assuming I made an error during the certificate creation and/or installation but I don't know what I may have done wrong.<br>
  Any ideas?</p>