     {:ok, ns}
   end
 
-  def ping(self) do send self, {:command, {:ping}}; :ok  end
+  def ping(self) do send self, {:command, {:ping}}; :ok end
   def pong(self) do send self, {:command, {:pong}}; :ok end
   def ok(self) do send self, {:command, {:ok}}; :ok end
   def info(self, map) do send self, {:command, {:info, map}}; :ok end
   def connect(self, map) do send self, {:command, {:connect, map}}; :ok end
-  def error(self, msg) do send self, {:command, {:err, msg}} end
+  def error(self, msg) do send self, {:command, {:err, msg}}; :ok end
   def subscribe(self, subject) do subscribe(self, subject, nil, subject) end
   def subscribe(self, subject, sid) do subscribe(self, subject, nil, sid) end
   def subscribe(self, subject, queue, sid) do

                         host: @default_host, port: @default_port,
                         socket_opts: [:binary, active: true]},
                 ps: nil}
  def start_link do
    start_link(@default_host, @default_port)
  end

  def start_link(host, port \\ @default_port) do
    state = @start_state
    state = %{state | opts: %{state.opts | host: host, port: port}}

  def msg(self, subject, sid, reply_queue, what) do
    send self, {:command, {:msg, subject, sid, reply_queue, what}}
  end
  def handle_info({:command, cmd}, state) do
    pack = Nats.Parser.encode(cmd)
    case :gen_tcp.send(state.sock, pack) do
      # case :ssl.send(state.socket, pack) do
      :ok -> :ok # IO.puts "sent #{inspect(pack)}..."
      {:error, :closed} -> IO.puts "socket closed: ignoring for NOW!!"
      oops -> IO.puts "NATS send unexpected result: #{inspect(oops)}"
    end
    {:noreply, state}
  end
  def handle_info({:tcp_closed, _sock}, state) do { :noreply, state } end
  def handle_info({:tcp_error, _sock, _reason}, state) do
    { :noreply, state }
  end
  def handle_info({:tcp_passive, _sock}, state) do { :noreply, state } end
  def handle_info({:tcp, _sock, data}, state) do handle_packet(state, data) end
  def handle_info({:ssl, _sock, data}, state) do handle_packet(state, data) end
  defp handle_packet(state, <<>>) do {:noreply, state} end
  defp handle_packet(state, packet) do
    pres = Nats.Parser.parse(state.ps, packet)
#    IO.puts "received NATS packet: #{inspect(pres)}; raw: #{inspect(packet)}"
    case pres do
      {:ok, msg, rest, nps} ->
        {:noreply, ns } = handle_packet1(%{state | ps: nps}, msg)
        handle_packet(ns, rest)
      other -> nats_err(state, "invalid parser result: #{inspect(other)}")
    end
  end
  defp handle_packet1(state, msg) do
    case msg do
      {:info, json} -> nats_info(state, json)
      {:connect, json} -> nats_connect(state, json)
