Source:
private Socket connect(String host, String port) throws IOException, InterruptedException { if(tunnel!=null) { String[] tokens = tunnel.split(":",3); if(tokens.length!=2) throw new IOException("Illegal tunneling parameter: "+tunnel); if(tokens[0].length()>0) host = tokens[0]; if(tokens[1].length()>0) port = tokens[1]; } String msg = "Connecting to " + host + ':' + port; events.status(msg); int retry = 1; while(true) { boolean isHttpProxy = false; InetSocketAddress targetAddress = null; try { Socket s = null; targetAddress = Util.getResolvedHttpProxyAddress(host, Integer.parseInt(port)); if(targetAddress == null) { targetAddress = new InetSocketAddress(host, Integer.parseInt(port)); } else { isHttpProxy = true; } s = new Socket(); s.connect(targetAddress); s.setTcpNoDelay(true); s.setSoTimeout(30*60*1000); if (isHttpProxy) { String connectCommand = String.format("CONNECT %s:%s HTTP/1.1\r\nHost: %s\r\n\r\n", host, port, host); s.getOutputStream().write(connectCommand.getBytes("UTF-8")); BufferedInputStream is = new BufferedInputStream(s.getInputStream()); String line = readLine(is); String[] responseLineParts = line.split(" "); if(responseLineParts.length < 2 || !responseLineParts[1].equals("200")) throw new IOException("Got a bad response from proxy: " + line); while(!readLine(is).isEmpty()) { } } return s; } catch (IOException e) { if(retry++>10) { String suffix = ""; if(isHttpProxy) { suffix = " through proxy " + targetAddress.toString(); } throw (IOException) new IOException("Failed to connect to " + host + ':' + port + suffix).initCause(e); } Thread.sleep(1000*10); events.status(msg+" (retrying:"+retry+")",e); } } }

Target:
private Socket connect(String host, String port) throws IOException, InterruptedException { if(tunnel!=null) { String[] tokens = tunnel.split(":",3); if(tokens.length!=2) throw new IOException("Illegal tunneling parameter: "+tunnel); if(tokens[0].length()>0) host = tokens[0]; if(tokens[1].length()>0) port = tokens[1]; } String msg = "Connecting to " + host + ':' + port; events.status(msg); int retry = 1; while(true) { boolean isHttpProxy = false; InetSocketAddress targetAddress = null; try { Socket s = null; targetAddress = Util.getResolvedHttpProxyAddress(host, Integer.parseInt(port)); if(targetAddress == null) { targetAddress = new InetSocketAddress(host, Integer.parseInt(port)); } else { isHttpProxy = true; } s = new Socket(); s.connect(targetAddress); s.setTcpNoDelay(true); s.setSoTimeout(30*60*1000); if (isHttpProxy) { String connectCommand = String.format("CONNECT %s:%s HTTP/1.1\r\nHost: %s\r\n\r\n", host, port, host); s.getOutputStream().write(connectCommand.getBytes("UTF-8")); BufferedInputStream is = new BufferedInputStream(s.getInputStream()); String line = readLine(is); String[] responseLineParts = line.split(" "); if(responseLineParts.length < 2 || !responseLineParts[1].equals("200")) throw new IOException("Got a bad response from proxy: " + line); while(!readLine(is).isEmpty()) { } } return s; } catch (IOException e) { if(retry++>10) { String suffix = ""; if(isHttpProxy) { suffix = " through proxy " + targetAddress.toString(); } throw new IOException("Failed to connect to " + host + ':' + port + suffix, e); } Thread.sleep(1000*10); events.status(msg+" (retrying:"+retry+")",e); } } }