   use GenServer
   require Logger
   alias AmqpOne.Transport.Socket
+  alias AmqpOne.Transport.Frame
 
   @type perf_t :: :open | :begin | :attach | :flow | :transfer |
   :disposition | :detch | :end | :close

    AmqpOne.Transport.Socket.set_active_once(state.socket)
    put_in state, :state, :hdr_exch
  end
  def connection(:hdr_exch, %Frame{performative: :open} = f, state) do
    :ok = AmqpOne.Transport.Socket.send(state.socket, encode_frame(f, f.channel))
    put_in state, :state, :open_sent
  end
  def connection(:hdr_exch, {:tcp, %Frame{performative: :open} = f}, state) do
    Logger.info "Got an open frame: #{inspect f}"
    put_in state, :state, :open_rcvd
  end
  def connection(:open_rcvd, %Frame{performative: :open} = f, state) do
    :ok = AmqpOne.Transport.Socket.send(state.socket, encode_frame(f, f.channel))
    put_in state, :state, :opened
  end
  def connection(:open_sent, {:tcp, %Frame{performative: :open} = f}, state) do
    Logger.info "Got an open frame: #{inspect f}"
    put_in state, :state, :opened
  end
  def connection(:open_sent, %Frame{performative: :close} = f, state) do
    Logger.info "Got an internal close frame: #{inspect f}"
    put_in state, :state, :close_pipe
  end
  def connection(:opened, %Frame{performative: :close} = f, state) do
    Logger.info "Got an internal close frame: #{inspect f}"
    put_in state, :state, :close_sent
  end
  def connection(:open_sent, {:tcp, %Frame{performative: :close} = f}, state) do
    Logger.info "Got an close frame: #{inspect f}"
    put_in state, :state, :close_rcvd
  end
  def connection(:opened, some_frame, state) do
    Logger.info "Got any AMQP frame: #{inspect some_frame}"
    Logger.error "We shoudl send this frame to someone else"
    state
  end
  def connection(:close_sent, {:tcp, %Frame{performative: :close} = f}, state) do
    AmqpOne.Transport.Socket.close(state.socket)
    put_in state, :state, :end
  end
  def connection(:close_rcvd, %Frame{performative: :close} = f, state) do
    AmqpOne.Transport.Socket.close(state.socket)
    put_in state, :state, :end
  end
  def conncetion(:open_pipe, %Frame{performative: :close} = f, state) do
    put_in state, :state, :oc_pipe
  end
  def conncetion(:open_pipe, @amqp_header, state) do
    put_in state, :state, :open_sent
  end
  def conncetion(:close_pipe, {:tcp, %Frame{performative: :open} = f}, state) do
    Logger.info "Got an open frame: #{inspect f}"
    put_in state, :state, :close_sent
  end

  Encodes the internal representation of a frame to a proper
  binary encoded AMQP 1.0 frame
  """
  @spec encode_frame(any, non_neg_integer) :: binary
  def encode_frame(frame, channel) when is_integer(channel) and channel < 65636 do
    payload = :erlang.term_to_binary(frame)
    << 2 :: integer,
