 
         # Once the TCP socket successfully connects, we can begin the
         # handshake with the server to establish the connection on the
-        # server. This is where we decide what protocol to use, and
-        # send things like the authKey etc.
+        # server.
         @rawSocket.once 'connect', =>
-
             # The protocol specifies that the magic number for the
             # version should be given as a little endian 32 bit
             # unsigned integer. The value is given in the `proto-def`

# Note that it's a plain JavaScript file, not a CoffeeScript file.
protodef = require('./proto-def')

# Each version of the protocol has a magic number specified in
# `./proto-def.coffee`. The most recent version is 4. Generally the
# official driver will always be updated to the newest version of the
# protocol, though RethinkDB supports older versions for some time.
protoVersion = protodef.VersionDummy.Version.V0_4

# We are using the JSON protocol for RethinkDB, which is the most
# recent version. The older protocol is based on Protocol Buffers, and

            version = new Buffer(4)
            version.writeUInt32LE(protoVersion, 0)

            # Send the protocol type that we will be using to
            # communicate with the server. Json is the only currently
            # supported protocol.
            protocol = new Buffer(4)
            protocol.writeUInt32LE(protoProtocol, 0)

            r_string = new Buffer(crypto.randomBytes(18)).toString('base64')

            @rawSocket.username = host["username"]
            @rawSocket.password = host["password"]

            # Default to admin user with no password if none is given.
            if @rawSocket.username is undefined
                @rawSocket.username = "admin"
            if @rawSocket.password is undefined
                @rawSocket.password = ""

            client_first_message_bare = "n=" + @rawSocket.username + ",r=" + r_string

            message = JSON.stringify({
                protocol_version: protoVersionNumber,
                authentication_method: "SCRAM-SHA-256",
                authentication: "n,," + client_first_message_bare})

            nullbyte = new Buffer('\0', "binary")

            @rawSocket.write Buffer.concat([version, Buffer(message.toString()), nullbyte])

            # Now we have to wait for a response from the server
            # acknowledging the new connection. The following callback
            # will be invoked when the server sends the first few
            # bytes over the socket.

            state = 1
            min = 0
            max = 0
            server_first_message = ""
            server_signature = ""
            auth_r = ""
            auth_salt = ""
            auth_i = 0

            xor_bytes = (a, b) ->
                res = []
                len = Math.min(a.length, b.length)
                for i in [0...len]
                    res.push(a[i] ^ b[i])
                return new Buffer(res)

            compare_digest = (a, b) ->
                left = undefined
                right = b
                result = undefined
                if a.length is b.length
                    left = a
                    result = 0
                else
                    left = b
                    result = 1

                len = Math.min(a.length, b.length)
                for i in [0...len]
                    result |= xor_bytes(a[i],b[i])

                return result is 0

            handshake_error = (code, message) =>
                if 10 <= code <= 20
                    throw new err.ReqlAuthError(message)
                else
                    throw new err.ReqlDriverError(message)

            handshake_callback = (buf) =>
                # Once we receive a response, extend the current
                # buffer with the data from the server. The reason we
