private void recieveHandshake() throws IOException {
        ByteBuffer ch = ByteBuffer.allocate((this.remoteHandshake != null ? this.remoteHandshake.capacity() : 0) + this.buffer.capacity());
        if (this.remoteHandshake != null) {
            this.remoteHandshake.rewind();
            ch.put(this.remoteHandshake);
        }
        ch.put(this.buffer);
        this.remoteHandshake = ch;

        // If the ByteBuffer ends with 0x0D 0x0A 0x0D 0x0A
        // (or two CRLFs), then the client handshake is complete
        byte[] h = this.remoteHandshake.array();
        if ((h.length>=4 && h[h.length-4] == CR
                        && h[h.length-3] == LF
                        && h[h.length-2] == CR
                        && h[h.length-1] == LF) ||
            (h.length==23 && h[h.length-1] == 0)) {
            completeHandshake();
        }
    }