code: function(isGateway) {
  
          // Remove the host/app/instance params if connecting directly.
          if (!isGateway) {
            delete monitorJSON.hostName;
            delete monitorJSON.appName;
            delete monitorJSON.appInstance;
          }
  
          // Define the connect/error listeners
          var onConnect = function() {
            removeListeners();
            callback(null, connection);
          };
          var onError = function(err) {
            removeListeners();
            log.error('connect.error', err);
            callback({msg: 'connection error', err:err});
          };
          var removeListeners = function() {
            connection.off('connect', onConnect);
            connection.off('error', onError);
          };
  
          // Wait if the connection is still awaiting connect
          if (connection && connection.connecting) {
            connection.on('connect', onConnect);
            connection.on('error', onError);
            return;
          }
  
          // Re-try if disconnected
          if (connection && !connection.connected) {
            connection.on('connect', onConnect);
            connection.on('error', onError);
            return connection.connect(callback);
          }
  
          // Verified connection
          return callback(null, connection);
        }
docs: Return a found connection immediately if it's connected. If connecting, wait for connection to complete. If not connected (and not connecting) re-try the connection.