                             # callback passed to the `r.connect`
                             # function.
                             @emit 'connect'
-                            return
-                        else if status_str == HANDSHAKE_AUTHFAIL
-                            # Since there's a special error for
-                            # authorization failure, we have to look
-                            # for this specific error message.
-                            @emit 'error',
-                                new err.ReqlAuthError(
-                                    """Could not connect to #{@host}:#{@port}, \
-                                    incorrect authentication key.""")
                         else
-                            # The protocol dictates that any other
-                            # string but `SUCCESS` is an error
-                            # indicating the problem, and that we
-                            # should report the error to the user.
-                            @emit 'error', new err.ReqlDriverError "Server dropped connection with message: \"" + status_str.trim() + "\""
-                            return
+                            throw new err.ReqlDriverError("Unexpected handshake state")
+
+                # We may have more messages if we're in the middle of the handshake,
+                # so we have to update the buffer.
+                @buffer = @buffer.slice(j + 1)
+                # This is the end of the handshake callback.
 
             # Register the handshake callback on the socket. Once the
             # handshake completes, it will unregister itself and

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

            # Since the auth key has a variable length, we need to
            # both encode its length in the wire format (little endian
            # 32 bit), and the bytes themselves.
            auth_buffer = new Buffer(@authKey, 'ascii')
            auth_length = new Buffer(4)
            auth_length.writeUInt32LE(auth_buffer.length, 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)

            # Write the version, auth key length, auth key, and
            # protocol number to the socket, in that order.
            @rawSocket.write Buffer.concat([version, auth_length, auth_buffer, protocol])

            # 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.
            handshake_callback = (buf) =>
                # Once we receive a response, extend the current
                # buffer with the data from the server. The reason we

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

crypto = require("crypto")

# 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.V1_0
protoVersionNumber = 0

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