title: Is a CONNECT request required when connecting to an HTTPS url over TcpClient/Sockets via SOCKS proxy?
body: <p>I am trying to connect to www.google.com:443 via SOCKS5 proxy over TcpClient, but am confused on why my CONNECT request is hanging when proxied. The code provided below will work without issue until the connection is proxied via SOCKS5 (if a regular HTTP proxy is used, it will work fine). </p>
  
  <pre><code>Public Sub SocksProxyTest(proxy As String, proxyPort As String)
      Const host As String = "www.google.com"
      Const hostPort As Integer = 443
  
      Dim buffer As Byte() = New Byte(2047) {}
      Dim bytes As Integer
      Dim client As TcpClient = New TcpClient(proxy, proxyPort)
  
      Dim stream As NetworkStream = client.GetStream()
  
      Debug.Print("negotiating socks5 connection")
      Using br = New BinaryReader(stream, Encoding.[Default], True) 
          Dim ip = Dns.GetHostAddresses(host)(0)
  
          Dim auth As Byte() = New Byte() {&amp;H5, &amp;H1, &amp;H0}
          Debug.Print("&gt;&gt; " &amp; String.Join(" ", auth.Select(Function(b) "&amp;H" &amp; b.ToString("X2"))))
          stream.Write(auth, 0, auth.Length)
  
          Dim result As Byte() = br.ReadBytes(2)
          Debug.Print("&lt;&lt; &amp;H" &amp; result(1).ToString("X2"))
  
          Dim data As Byte() = {&amp;H5, &amp;H1, &amp;H0, &amp;H1, &amp;H0, &amp;H0, &amp;H0, &amp;H0, get_port_bytes(hostPort)(0), get_port_bytes(hostPort)(1)}
          For i As Integer = 0 To 4 - 1
              data(i + 4) = ip.GetAddressBytes()(i)
          Next
          Debug.Print("&gt;&gt; " &amp; String.Join(" ", data.Select(Function(b) "&amp;H" &amp; b.ToString("X2"))))
  
          stream.Write(data, 0, data.Length)
          stream.Flush()
  
          result = br.ReadBytes(10)
          Debug.Print("&lt;&lt; &amp;H" &amp; result(1).ToString("X2"))
  
          br.Close()
      End Using
      Debug.Print("negotiated socks5 connection")
  
      Debug.Print("sending tunnel request")
      Dim tunnelRequest As Byte() = Encoding.UTF8.GetBytes(String.Format("CONNECT {0}:{1}  HTTP/1.1{2}Host: {0}{2}{2}", host, hostPort, vbCrLf))
      stream.Write(tunnelRequest, 0, tunnelRequest.Length)
      stream.Flush()
  
      bytes = stream.Read(buffer, 0, buffer.Length)
      Console.Write(Encoding.UTF8.GetString(buffer, 0, bytes))
  
      Dim sslStream As SslStream = New SslStream(stream)
      sslStream.AuthenticateAsClient(host)
  
      Debug.Print("sending web request")
      Dim request As Byte() = Encoding.UTF8.GetBytes(String.Format("GET https://{0}/  HTTP/1.1" &amp; vbCrLf &amp; "Host: {0}" &amp; vbCrLf &amp; vbCrLf, host))
      sslStream.Write(request, 0, request.Length)
      sslStream.Flush()
  
      Do
          bytes = sslStream.Read(buffer, 0, buffer.Length)
          Debug.Print(Encoding.UTF8.GetString(buffer, 0, bytes))
      Loop While bytes &lt;&gt; 0
  
      client.Close()
  End Sub
  </code></pre>