public NonBlockingSocket(URI uri, SocketFactory factory,
            BOSHClientSocketConnectorFactory socketConnectorFactory, SSLConnector sslConnector) {
        this.uri = uri;
        this.sslConnector = sslConnector;

        try {
            socket = factory.createSocket();
            socketConnector = socketConnectorFactory.createConnector(socket);
        } catch(IOException e) {
            // In the unlikely case that creating the socket itself fails, stash
            // the error and it'll be returned from the first close().
            error = e;
            closed = true;
            return;
        }
        
        Runnable r = new Runnable() {
            public void run() {
                threadMain();
            }
        };

        thread = new Thread(r);
        thread.setName("NonBlockingSocket thread: " + uri.getHost() + ":" + uri.getPort());
        thread.start();
    }