     Logger.debug "Received event #{inspect event}"
     if receiver == nil do
       # enqueue the event
-      new_q = :queue.in(q, event)
+      new_q = :queue.in(event, q)
       %__MODULE__{state | messages: new_q}
     else
       # send the event directly to the waiting process


  @spec hci_receive(non_neg_integer) :: {:ok, any} | {:error, any}
  def hci_receive(timeout \\ 5_000) when is_integer(timeout) and timeout >= 0 do
    GenServer.call(__MODULE__, {:hci_recieve, [timeout]})
  end



    Logger.debug "State will be #{inspect state}"
    {:ok, state}
  end
  def init([pid]) when is_pid(pid) do
    Logger.debug("HCI.init with emulator pid #{inspect pid}")
    state = %__MODULE__{port: pid}
    Logger.debug "State will be #{inspect state}"
    {:ok, state}
  end

  def handle_call({:hci_receive, [timeout]}, from, s = %__MODULE__{messages: q, receiver: nil}) do
    case :queue.out(q) do
