function registerHandlersAndDomains(ws, port) {
            // Called if we succeed at the final setup
            function success() {
                self._ws.onclose = function () {
                    if (self._autoReconnect) {
                        var $promise = self.connect(true);
                        self.trigger("close", $promise);
                    } else {
                        self._cleanup();
                        self.trigger("close");
                    }
                };
                deferred.resolve();
            }
            // Called if we fail at the final setup
            function fail(err) {
                self._cleanup();
                deferred.reject(err);
            }

            self._ws = ws;
            self._port = port;
            self._ws.onmessage = self._receive.bind(self);

            // refresh the current domains, then re-register any
            // "autoregister" modules
            self._refreshInterface().then(
                function () {
                    if (self._registeredModules.length > 0) {
                        self.loadDomains(self._registeredModules, false).then(
                            success,
                            fail
                        );
                    } else {
                        success();
                    }
                },
                fail
            );
        }
Connect to the node server. After connecting, the NodeConnection
object will trigger a "close" event when the underlying socket
is closed. If the connection is set to autoReconnect, then the
event will also include a jQuery promise for the connection.

@param {boolean} autoReconnect Whether to automatically try to
   reconnect to the server if the connection succeeds and then
   later disconnects. Note if this connection fails initially, the
   autoReconnect flag is set to false. Future calls to connect()
   can reset it to true
@return {jQuery.Promise} Promise that resolves/rejects when the
   connection succeeds/fails