     setEncoding: (e) ->
       encoding = e
     write: (data) ->
-      performWrite = ->
-        req = http.request({port: phantomServerPort, method: "POST"})
+      writeQueue.push ->
+        req = http.request {port: phantomServerPort, method: "POST"}, ->
+          waitingOnPreviousWrite = false
+          doNextWrite()
         req.on "error", (err) ->
           logger.error?(err)
         req.setHeader("Content-Length", data.length)
         req.end(data)
       if readyForWriting
-        performWrite()
+        doNextWrite()
       else
         emitter.on "ready", ->
           readyForWriting = true
-          performWrite()
+          doNextWrite()
   return stream
 
 exports.open = open

  readyForWriting = false
  encoding = null
  emitter = new EventEmitter()
  stream =
    readable: true
    writeable: true


  console.info("CLIENT: SEND: hello world")
  stream.write("hello world")
  stream.write("hello world2")
  stream.setEncoding("utf-8")
  stream.on "data", (data) ->
    console.info("CLIENT: RECV: #{data}")
