       # Else its body parts
       else if data.length > 0
         @emit 'data', data
+
+      # Empty string mean EOF
       else
         @emit 'end'
 

#
exports.Client = class Client extends Stream
  constructor: ->
    @_outgoing = []
    @_incoming = null

    @on 'connect', => @_processRequest()
    @on 'close',   => @_finishRequest()

    @_initResponseParser()

  _initResponseParser: () ->
    # Initialize a Netstring stream parser
    nsStream = new ns.Stream @

    nsStream.on 'data', (data) =>
      if @_incoming
        @_incoming._receiveData data

    nsStream.on 'error', (exception) =>
      @_incoming = null
      @emit 'error', exception

  _processRequest: () ->
    if @readyState is 'open' and !@_incoming
      if request = @_outgoing[0]
        @_incoming = new ClientResponse @, request
        request.flush()
    else
      @reconnect()

  _finishRequest: () ->
    @_outgoing.shift()
    @_incoming = null

    if @_outgoing.length > 0
      @_processRequest()


    while @_writeQueue and @_writeQueue.length
      data = @_writeQueue.shift()

      # Close write socket when we see an empty netstring `0:,`
      if data is END_OF_FILE
        @socket.end data
      else
        @socket.write data

    # Clear queue, remaining writes won't buffer
    @_writeQueue = null

    # Buffer is empty, let the world know!
