title: How to set SOCKS proxy to websocket-client
body: <p>I'm trying to set a SOCKS proxy to the <code>websocket-client</code>'s <code>WebSocket</code> with <code>create_connection</code> but It always keeps closing the socket after websocket sends the handshake request.</p>
  <pre><code>Traceback (most recent call last):
    File &quot;D:\PyCharmProjects\x-python\venv\lib\site-packages\websocket\_core.py&quot;, line 252, in connect
      self.handshake_response = handshake(self.sock, *addrs, **options)
    File &quot;D:\PyCharmProjects\x-python\venv\lib\site-packages\websocket\_handshake.py&quot;, line 79, in handshake
      status, resp = _get_resp_headers(sock)
    File &quot;D:\PyCharmProjects\x-python\venv\lib\site-packages\websocket\_handshake.py&quot;, line 162, in _get_resp_headers
      status, resp_headers, status_message = read_headers(sock)
    File &quot;D:\PyCharmProjects\x-python\venv\lib\site-packages\websocket\_http.py&quot;, line 308, in read_headers
      line = recv_line(sock)
    File &quot;D:\PyCharmProjects\x-python\venv\lib\site-packages\websocket\_socket.py&quot;, line 134, in recv_line
      c = recv(sock, 1)
    File &quot;D:\PyCharmProjects\x-python\venv\lib\site-packages\websocket\_socket.py&quot;, line 125, in recv
      raise WebSocketConnectionClosedException(
  websocket._exceptions.WebSocketConnectionClosedException: Connection is already closed.
  python-BaseException
  
  Process finished with exit code -1
  
  </code></pre>
  <p>And that's how I'm trying to wrap the socket with the PySocks.</p>
  <pre class="lang-py prettyprint-override"><code>from socks import socksocket
  proxied_sock = socksocket()
  proxied_sock.setsockopt(IPPROTO_TCP, TCP_NODELAY, 1)
  proxied_sock.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
  proxied_sock = self.__proxy.set_to_socket(proxied_sock)
  proxied_sock.connect((dealer_str, 80))
  self.__logger.debug(&quot;WS wrapped with proxy!&quot;)
  self.__ws = create_connection(self.__dealer_uri, socket=proxied_sock)
  </code></pre>
  <p>The <code>set_to_socket()</code> function is just a function to wrap the proxy.</p>
  <pre class="lang-py prettyprint-override"><code>    def set_to_socket(self, s) -&gt; socksocket:
          &quot;&quot;&quot;
          Sets socket's proxy
          :param s: socket.socket
          :return: Proxied socket.socket
          &quot;&quot;&quot;
          if self.proxy_type == &quot;http&quot; or self.proxy_type == &quot;https&quot;:
              s.set_proxy(HTTP, self.host, self.port, username=self.username, password=self.password)
          elif self.proxy_type == &quot;socks4&quot;:
              s.set_proxy(SOCKS4, self.host, self.port, username=self.username, password=self.password)
          elif self.proxy_type == &quot;socks5&quot;:
              s.set_proxy(SOCKS5, self.host, self.port, username=self.username, password=self.password)
          else:
              raise ProxyError(&quot;Proxy type not found: &quot; + self.proxy_type)
  
          return s
  </code></pre>
  <p>Why is that happening and how can I wrap my websocket with SOCKS proxy ?</p>