title: SOCKS5 Proxy using SSLSocket
body: <p>I have a client/server application that remotely connects to a server via Java's SSLSocket.</p>
  
  <p>I'm trying to implement an optional mode that enables connections via an authenticated SOCKS v5 proxy.</p>
  
  <p>I tried using the <a href="http://download.oracle.com/javase/6/docs/technotes/guides/net/proxies.html" rel="nofollow">relevant tutorial</a> but it doesn't mention anything about SSL specifically.</p>
  
  <p>I've tried setting the system-wide properties ("socksProxyHost" and "socksProxyPort") but it doesn't seem to do anything.</p>
  
  <p>My next approach was to use factory method in SSLSocketFactory:</p>
  
  <pre><code>            String proxyHost = Config.prefs.get("PROXY_NAME", "localhost");
              int proxyPort = Config.prefs.getInt("PROXY_PORT", 1080);
              InetSocketAddress proxyAddr = new InetSocketAddress(proxyHost, proxyPort);
              Socket underlying = new SSLSocket(new Proxy(Proxy.Type.SOCKS, proxyAddr));
              underlying.connect(new InetSocketAddress(getHost(), getPort()));
              socket = (SSLSocket) factory.createSocket(
                      underlying,
                      getHost(),
                      getPort(),
                      true);
  </code></pre>
  
  <p>But the problem with this approach is that the createSocket method requires the underlying socket to be already connected, and my server won't accept non SSL connections.</p>
  
  <p>What's the best way to connect to my remote server using SOCKS?  Also, I have next to no idea how to supply a username/password for authenticated SOCKS in this system.</p>
  
  <p>Thanks!</p>