private void OnConnectSink(IAsyncResult ar) { if (wsclient == null) { return; } // Accept the connection try { wsclient.EndConnect(ar); } catch (Exception ex) { parent.Debug("#" + counter + ": Websocket TCP failed to connect: " + ex.ToString()); parent.ShutdownClients(client, uclient, this, this.counter); return; } if (proxyInUse == true) { // Send proxy connection request wsrawstream = wsclient.GetStream(); byte[] proxyRequestBuf = UTF8Encoding.UTF8.GetBytes("CONNECT " + url.Host + ":" + url.Port + " HTTP/1.1\r\nHost: " + url.Host + ":" + url.Port + "\r\n\r\n"); wsrawstream.Write(proxyRequestBuf, 0, proxyRequestBuf.Length); wsrawstream.BeginRead(readBuffer, readBufferLen, readBuffer.Length - readBufferLen, new AsyncCallback(OnProxyResponseSink), this); } else { // Start TLS connection parent.Debug("#" + counter + ": Websocket TCP connected, doing TLS..."); wsstream = new SslStream(wsclient.GetStream(), false, VerifyServerCertificate, null); wsstream.BeginAuthenticateAsClient(url.Host, null, System.Security.Authentication.SslProtocols.Tls12, false, new AsyncCallback(OnTlsSetupSink), this); } }