title: TcpListener write to ClientWebSocket
body: <p>Im currently creating a project that will run in a browser &amp; has an c# server connected.</p>
  <p>The Server uses an TcpListener to accept connections &amp; receive messages, but I want the server to be able to respond to the client. This has given me a few issues.</p>
  <p>Here is the code for my client:</p>
  <pre><code>        private ClientWebSocket socket;
  
          internal async Task InitAsync(string host, int port, GamePacketParser parser)
          {
              Logger.Info(&quot;Setting up the socket connection...&quot;);
  
              socket = new ClientWebSocket();
              await socket.ConnectAsync(new Uri($&quot;ws://{host}:{port}/&quot;), CancellationToken.None);
  
              Logger.Info(&quot;Successfully established the connection.&quot;);
              this.parser = parser;
              buffer = new byte[GameSocketManagerStatics.BUFFER_SIZE];
  
              Task.Run(recieve);
          }
  
          private async Task recieve()
          {
              Logger.Debug(&quot;Starting Reciever.....&quot;);
              var result = await socket.ReceiveAsync(new ArraySegment&lt;byte&gt;(buffer), CancellationToken.None);
  
              var packet = new byte[result.Count];
              Array.Copy(buffer, packet, result.Count);
  
              ///parser.handlePacketData(packet);
  
              Logger.Debug($&quot;Recieved: {Encoding.UTF8.GetString(packet)}&quot;);
              ///Task.Run(recieve); //Start receiving again
          }
  
          public async Task SendData(byte[] data)
          {
              Logger.Debug(&quot;Triggerd send&quot;);
              string packet = BitConverter.ToString(data);
              await socket.SendAsync(new ArraySegment&lt;byte&gt;(data), WebSocketMessageType.Text, true, CancellationToken.None);
              Logger.Info($&quot;Sended Data: {packet}&quot;);
          }
  
  
  </code></pre>
  <p>The code above simply connects to the server over a web socket. Sending packets works fine. The second the server sends data back, the client won't send any data anymore to the server. Like its stuck.</p>
  <pre><code>        static void Main(string[] args)
          {
              string ip = &quot;127.0.0.1&quot;;
              int port = 30000;
              var server = new TcpListener(IPAddress.Parse(ip), port);
  
              server.Start();
              Console.WriteLine(&quot;Server has started on {0}:{1}, Waiting for a connection...&quot;, ip, port);
  
              TcpClient client = server.AcceptTcpClient();
              Console.WriteLine(&quot;A client connected.&quot;);
  
              NetworkStream stream = client.GetStream();
  
              if (Regex.IsMatch(s, &quot;^GET&quot;, RegexOptions.IgnoreCase))
              {
                      Console.WriteLine(&quot;=====Handshaking from client=====\n{0}&quot;, s);
  
                      // 1. Obtain the value of the &quot;Sec-WebSocket-Key&quot; request header without any leading or trailing whitespace
                      // 2. Concatenate it with &quot;258EAFA5-E914-47DA-95CA-C5AB0DC85B11&quot; (a special GUID specified by RFC 6455)
                      // 3. Compute SHA-1 and Base64 hash of the new value
                      // 4. Write the hash back as the value of &quot;Sec-WebSocket-Accept&quot; response header in an HTTP response
                      string swk = Regex.Match(s, &quot;Sec-WebSocket-Key: (.*)&quot;).Groups[1].Value.Trim();
                      string swka = swk + &quot;258EAFA5-E914-47DA-95CA-C5AB0DC85B11&quot;;
                      byte[] swkaSha1 = System.Security.Cryptography.SHA1.Create().ComputeHash(Encoding.UTF8.GetBytes(swka));
                      string swkaSha1Base64 = Convert.ToBase64String(swkaSha1);
  
                      // HTTP/1.1 defines the sequence CR LF as the end-of-line marker
                      byte[] response = Encoding.UTF8.GetBytes(
                          &quot;HTTP/1.1 101 Switching Protocols\r\n&quot; +
                          &quot;Connection: Upgrade\r\n&quot; +
                          &quot;Upgrade: websocket\r\n&quot; +
                          &quot;Sec-WebSocket-Accept: &quot; + swkaSha1Base64 + &quot;\r\n\r\n&quot;);
  
                      stream.Write(response, 0, response.Length);
              }
  
              byte[] message = Encoding.UTF8.GetBytes(&quot;Connection is established&quot;);
              stream.Write(message, 0, message.Length);
  
           }
  </code></pre>
  <p>The problem is probably because it is not encoded for WebSockets, but I tried a lot of online solutions for encoding (For example: <a href="https://stackoverflow.com/questions/8125507/how-can-i-send-and-receive-websocket-messages-on-the-server-side">How can I send and receive WebSocket messages on the server side?</a>) But even with those encoders, it did not seem to solve the problem.</p>
  <p>Thanks for your help in advance. Im still new to WebSockets, so spoonfeeding is allowed.</p>