     {socket, port}
   end
 
-  defp send_and_recv(socket, port, command) do
+  defp send_and_recv(command, socket, port) do
     :ok = :gen_udp.send(socket, '127.0.0.1', port, command)
     case :gen_udp.recv(socket, 0, @timeout) do
       {:ok, {_addr, _port, data}} -> data

    end
  end

  defp setup_game() do
    token = ~r/ROUND STARTING;([0-9a-fA-F]{32})\n/
    {socket1, port1} = open_udp_socket({127,0,0,1})
    {socket2, port2} = open_udp_socket({127,0,0,2})
    {socket3, port3} = open_udp_socket({127,0,0,3})
    send_and_recv(socket1, port1, "REGISTER;player1")
    send_and_recv(socket2, port2, "REGISTER;player2")
    send_and_recv(socket3, port3, "REGISTER_SPECTATOR")
    {:ok, {_ip, _port, invitation1}} = :gen_udp.recv(socket1, 0, @timeout)
    {:ok, {_ip, _port, invitation2}} = :gen_udp.recv(socket2, 0, @timeout)
    {:ok, {_ip, _port, "ROUND STARTING\n"}} = :gen_udp.recv(socket3, 0, @timeout)
    [^invitation1, mytoken1] = Regex.run(token, invitation1)
    [^invitation2, mytoken2] = Regex.run(token, invitation2)
    :gen_udp.send(socket1, 'localhost', port1, "JOIN;"<>mytoken1)
    :gen_udp.send(socket2, 'localhost', port2, "JOIN;"<>mytoken2)
    {:ok, {_ip, _port, _start1}} = :gen_udp.recv(socket1, 0, @timeout)
    {:ok, {_ip, _port, _start2}} = :gen_udp.recv(socket2, 0, @timeout)
    {:ok, {_i, _p, start}} = :gen_udp.recv(socket3, 0, @timeout)
    {start, [socket1, socket2, socket3], [port1, port2, port3]}
  end


  test "Player sends invalid command" do
    {startmsg, sockets, ports} = setup_game()
    {1, [player | _], [socket | _], [port | _]} = extract_player_seq(startmsg, sockets, ports)
    socket
      |> receive_message()
      |> check_and_gettoken(:yourturn)
      |> make_nonsense_msg()
      |> send_to_server(socket, port)
    check_player_lost_aftermath(player, sockets, "INVALID TURN")
  end

  test "Player rolls but fails to announce" do
    {startmsg, sockets, ports} = setup_game()
    {1, [player | _], [socket | _], [port | _]} = extract_player_seq(startmsg, sockets, ports)
    socket
      |> receive_message()
      |> check_and_gettoken(:yourturn)
      |> make_roll_msg()
      |> send_to_server(socket, port)
    check_broadcast_message(sockets, "PLAYER ROLLS;#{player}\n")
    receive_message(socket)
    Process.sleep(@timeout)
    check_player_lost_aftermath(player, sockets, "DID NOT ANNOUNCE")
  end

  test "Game round with consecutive announcements" do
    {startmsg, sockets, ports} = setup_game()
    {1, [player1, player2 | _], [socket1, socket2 | _], [port1, port2 | _]} = extract_player_seq(startmsg, sockets, ports)
    socket1
      |> receive_message()
      |> check_and_gettoken(:yourturn)
      |> make_roll_msg()
      |> send_to_server(socket1, port1)
    check_broadcast_message(sockets, "PLAYER ROLLS;#{player1}\n")
    MiaServer.Game.testing_inject_dice(MiaServer.Dice.new(3,1))
    socket1
      |> receive_message()
      |> check_and_gettoken(:rolled)
      |> make_announcement_msg(3,1)
      |> send_to_server(socket1, port1)
    check_broadcast_message(sockets, "ANNOUNCED;#{player1};3,1\n")
    # Second players turn
    socket2
      |> receive_message()
      |> check_and_gettoken(:yourturn)
      |> make_roll_msg()
      |> send_to_server(socket2, port2)
    check_broadcast_message(sockets, "PLAYER ROLLS;#{player2}\n")
    MiaServer.Game.testing_inject_dice(MiaServer.Dice.new(3,2))
    socket2
      |> receive_message()
      |> check_and_gettoken(:rolled)
      |> make_announcement_msg(4,1)
      |> send_to_server(socket2, port2)
    check_broadcast_message(sockets, "ANNOUNCED;#{player2};4,1\n")
    # Again first players turn
    socket1
      |> receive_message()
      |> check_and_gettoken(:yourturn)
  end

end
