title: C# Websockets - TCP Reverse proxy displaying websocket error
body: <p>Okay so I made a C# TCP reverse proxy server, and for some reason, other TCP reverse proxy servers work but mine does it. In the developer console, mine displays:</p>
  
  <pre><code>WebSocket connection to 'ws://127.0.0.1:1738/' failed: One or more reserved bits are on: reserved1 = 0, reserved2 = 1, reserved3 = 1
  </code></pre>
  
  <p>Here's the code:</p>
  
  <p>Socket.cs:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using System.Net.Sockets;
  
  namespace MINA
  {
      class Sock
      {
          Socket Client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
          string name = null;
          public Sock(string name)
          {
              this.name = name;
          }
          public void Connect(string IP, int port, Socket c = null)
          {
              this.Client.ReceiveTimeout = 0;
              if(c != null)
              {
                  this.Client = c;
                  return;
              }
              this.Client.Connect(IP, port);
              Output.Write("Connected to " + IP + ':' + port, name);
          }
  
          public string Send(string data, bool recv = true)
          {
              try
              {
                  this.Client.Send(Encoding.ASCII.GetBytes(data));
                  Output.Write(data, name + " | send");
                  if(recv)
                  {
                      return this.Receive();
                  } else
                  {
                      return null;
                  }
              } catch(SocketException)
              {
                  Output.Write("Socket client might not be able to connect to host, and therefore can not send any data.", "fail");
                  this.Client.Close();
                  return null;
              }
          }
          public bool isConnected = true;
          public string Receive()
          {
              try
              {
                  byte[] buffer = new byte[2048];
                  this.Client.Receive(buffer);
                  string Data = Encoding.ASCII.GetString(buffer).Replace("\0", "");
                  Output.Write(Data, name + " | recv");
                  return Data;
              }
              catch (Exception e)
              {
                  isConnected = false;
                  this.Client.Close();
                  return null;
              }
          }
  
          public void Close()
          {
              this.Client.Close();
          }
  
      }
  }
  </code></pre>
  
  <p>SocketServer.cs:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using System.Net.Sockets;
  using System.Net;
  using System.Threading;
  
  namespace MINA
  {
      class SocketServer
      {
          int BindPort = 0;
          Socket Server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
          public SocketServer(int BindPort = 443)
          {
              this.BindPort = BindPort;
          }
  
          public void BindListen()
          {
              this.Server.Bind(new IPEndPoint(IPAddress.Any, this.BindPort));
              Output.Write("Server bound to port " + this.BindPort, "server");
              this.Server.Listen(1000);
              Output.Write("Server listening for new connections", "server");
              while (true)
              {
                  Socket RCV = this.Server.Accept();
                  Thread t = new Thread(
                      new ThreadStart(
                          delegate
                          {
                              Output.Write("Client accepted, bombs fired.", "server");
                              Sock Receiver = new Sock("RECEIVER");
                              Receiver.Connect("0", 0, RCV);
  
  
  
                              Sock Forwarder = new Sock("FORWARDER");
                              Forwarder.Connect("127.0.0.1", 443);
                              while (Forwarder.isConnected &amp;&amp; Receiver.isConnected)
                              {
                                  handleReceiverReceive(Forwarder, Receiver);
                                  handleForwarderReceive(Forwarder, Receiver);
                              }
                              Forwarder.Close();
                              Receiver.Close();
                          }
                      )
                  );
                  t.Start();
              }
          }
  
          private void handleForwarderReceive(Sock Forwarder, Sock Receiver)
          {
              string forwarderReceive = Forwarder.Receive();
              if (forwarderReceive != null)
              {
                  Receiver.Send(forwarderReceive, false);
              }
              else
              {
                  Forwarder.Close();
                  Receiver.Close();
              }
          }
  
          private void handleReceiverReceive(Sock Forwarder, Sock Receiver)
          {
              string receiverReceive = Receiver.Receive();
              if (receiverReceive != null)
              {
                  Forwarder.Send(receiverReceive, false);
              }
              else
              {
                  Forwarder.Close();
                  Receiver.Close();
              }
          }
      }
  }
  </code></pre>