public void run() {
        try {
            int port = uri.getPort();
            if (port == -1) {
                port = 80;
            }

            // The WebSocket constructor expects a SocketChannel that is
            // non-blocking, and has a Selector attached to it.
            SocketChannel client = SocketChannel.open();
            client.configureBlocking(false);
            client.connect(new InetSocketAddress(uri.getHost(), port));

            Selector selector = Selector.open();

            this.conn = new WebSocket(client, this);
            client.register(selector, client.validOps());

            // Continuous loop that is only supposed to end when close is called
            while (selector.select(500) > 0) {

                Set<SelectionKey> keys = selector.selectedKeys();
                Iterator i = keys.iterator();

                while (i.hasNext()) {
                    SelectionKey key = (SelectionKey)i.next();
                    i.remove();

                    // When 'conn' has connected to the host
                    if (key.isConnectable()) {
                        // Ensure connection is finished
                        if (client.isConnectionPending())
                            client.finishConnect();

                        // Now send WebSocket client-side handshake
                        String path = "/" + uri.getPath();
                        String host = uri.getHost() + (port != 80 ? ":" + port : "");
                        String origin = null; // I don't know what to put here!?
                        String request = "GET "+path+" HTTP/1.1\r\n" +
                            "Upgrade: WebSocket\r\n" +
                            "Connection: Upgrade\r\n" +
                            "Host: "+host+"\r\n" +
                            "Origin: "+origin+"\r\n" +
                            //extraHeaders.toString() +
                            "\r\n";
                        conn.socketChannel().write(ByteBuffer.wrap(request.getBytes(WebSocket.UTF8_CHARSET)));
                    }

                    // When 'conn' has recieved some data
                    if (key.isReadable()) {
                        conn.handleRead();
                    }
                }
            }

        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }