     {:ok, state} = init([host, port, opts, transport])
     new_state = %__MODULE__{state | socket: socket}
     Logger.info "init(..,..,): state = #{inspect new_state}"
-    :gen_server.enter_loop(__MODULE__, [], new_state)
+    case transport.recv(socket, 8, 5_000) do
+      {:ok, @amqp_header} ->
+            :ok = transport.send(socket, @amqp_header)
+            :inet.setopts(socket, [active: :once, packet: 4])
+            :gen_server.enter_loop(__MODULE__, [], %__MODULE__{new_state| state: :hdr_exch})
+      _ -> transport.close(socket)
+    end
+
   end
 
   @doc false

    opts = options
    |> Keyword.put(:active, false)
    |> Keyword.put(:packet, 0)
    {:ok, pid} = GenServer.start_link(__MODULE__, [host, port, opts, socket_mod], [])
    GenServer.call(pid, {:connect})
  end


    AmqpOne.Transport.Socket.set_active_once(state.socket)
    put_in state, :state, :hdr_exch
  end
  def connection(:hdr_rcvd, {:tcp, header = @amqp_header}, state) do
    # this is currently a server state, our connection functions does
    # not allow this situation. Not tested!
    :ok = AmqpOne.Transport.Socket.send(state.socket, header)
