       error: new noflo.Port()
 
     # Close connection on application exit, SIGINT, and SIGTERM
-    endServer = _.bind(@endServer, this)
-    process.on("exit", endServer)
-    process.on("SIGINT", endServer)
-    process.on("SIGTERM", endServer)
-    process.on("SIGHUP", endServer)
-
-    @inPorts.quit.on "disconnect", =>
-      @endServer()
+    endServer = _.bind @endServer, this
+    process.on "exit", endServer
+    process.on "SIGINT", endServer
+    process.on "SIGTERM", endServer
+    process.on "SIGHUP", endServer
 
     @inPorts.token.on "data", (@token) =>
 
-    @inPorts.server.on "data", (url) =>
-      @startServer url
+    @inPorts.server.on "data", (@url) =>
+      @startServer @url
+    @inPorts.quit.on "data", (@url) =>
+      @endServer @url
 
     @inPorts.in.on "connect", =>
       @sqls = []
 
     @inPorts.in.on "data", (data) =>
-      @sqls.push(data)
+      @sqls.push data
 
     @inPorts.in.on "disconnect", =>
       token = @token
       query = _.flatten(@sqls).join(";\n")
 
-      unless client?
+      unless client[@url]?
         throw new Error "Server connection has not yet been established"
       unless token?
         throw new Error "Missing token for return connection"
       unless query?
         throw new Error "Missing query to execute"
 
-      result = client.query query
+      result = client[@url].query query
 
       # Send row forward
       result.on "row", (row, result) =>
-        result.addRow(row)
+        result.addRow row
 
       # Send to error port
       result.on "error", (e) =>
         unless @outPorts.error.isAttached()
           throw new Error "No error port attached"
 
-        @outPorts.error.send(e)
+        @outPorts.error.send e
         @outPorts.error.disconnect()
 
       result.on "end", (result) =>

        @outPorts.out.disconnect()

  startServer: (url) ->
    @endServer()
    client = new pg.Client(url)
    client.connect()

  endServer: ->
    client?.end()

exports.getComponent = -> new Client

pg = require("pg")
noflo = require("noflo")

# Pool all queries through a single connection for each database
client = {}

class Client extends noflo.Component

