private void setupConnection() throws Throwable {
		uri=new URI(url);
		
		// Detect protocol, host, port
		String hostHeader;
		String scheme=uri.getScheme();
		port=uri.getPort();
		hostName=uri.getHost();
		if ("ws".equalsIgnoreCase(scheme) || "http".equals(scheme)) {
			// Default to http
			if (port<0) port=80;
			if (port!=80) hostHeader=hostName + ':' + port;
			else hostHeader=hostName;
			socketFactory=netConfig.getPlainSocketFactory();
		} else if ("wss".equalsIgnoreCase(scheme) || "https".equalsIgnoreCase(scheme)) {
			// Secure
			if (port<0) port=443;
			if (port!=443) hostHeader=hostName + ':' + port;
			else hostHeader=hostName;
			socketFactory=netConfig.getSecureSocketFactory();
		} else {
			throw new IllegalArgumentException("Unsupported websocket protocol");
		}
		
		// Add the host header
		requestHeaders.put("Host", hostHeader);
		
		// Connect the socket
		socket=socketFactory.createSocket(hostName, port);
		try {
			// Buffer the streams to a typical network packet size
			in=new DataInputStream(new BufferedInputStream(socket.getInputStream(), 1500));
			out=new DataOutputStream(new BufferedOutputStream(socket.getOutputStream(), 1500));
		} catch (Throwable t) {
			socket.close();
			throw t;
		}
	}