 # * `host`: {String} hostname of affqis server.
 # * `port`: {Number} port number of affqis server.
 # * `cb`: {Function} callback to get called with an error if we
-#         fail to establish a connection, or a session if we succeed.
+#         fail to establish a connection, or an {Object} with
+#         keys for `connection` and `session`
 connect = (realm, host, port, cb) ->
   affqisUrl = "ws://#{host}:#{port}/affqis"
   connection = new autobahn.Connection

# * `cb`: {Function} callback that'll get called with errors or our results.
executeQuery = (session, id, hql, cb) ->
  rows = []
  session.call('execute', [id, hql])
   .then((topic) ->
     session.subscribe(topic, ([event, row]) ->
       if event == "row"
         rows.push JSON.parse(row)
       else
         cb null,
          rows: rows.map(normalizeRow)
          rowCount: rows.map.length
          fields: rows[0].map((data) -> _.pick data, ['name', 'type'])

    realm: realm
    max_retries: 0

  connection.onopen = (session) ->
    cb(null, session: session, connection: connection)

  connection.onclose = (reason) =>
    switch reason
