/// <summary>
        /// Begin round.
        /// </summary>
        /// <param name="host">The host.</param>
        public static void BeginRound(PirateHost host)
        {
            Contract.Requires(host != null && host.Game.Round.BetsDone);

            var bets = new HashSet<string>();
            foreach(var player in host.GetPlayers()) {
                bets.Add(PirateMessage.ConstructPlayerBet(player));
            }
            bets.Add(PirateMessage.ConstructRoundNumber(host.Game.CurrentRound));

            var msg = new PirateMessage(PirateMessageHead.Bgrn, PirateMessage.ConstructBody(bets));

            foreach(var player in host.GetPlayers()) {
                host.SendMessage(player, msg);
            }
            lock (host.Game.Round) {
                host.Game.Round.Begin();
            }
        }
        public void PlayGame()
        {
            PirateHost host = new PirateHost(4939);
            host.Start();
            host.Broadcaster.Interval = 1.1;
            Assert.That(host.Broadcaster.Interval < 1.11 && host.Broadcaster.Interval > 1.09);

            PirateClient player1 = new PirateClient("Player1", "127.0.0.1", 4939);
            player1.SetGame(new Game());
            player1.BetRequested += OnBetRequest;
            player1.CardRequested += OnCardRequest;
            player1.Disconnected += OnDisconnect;
            player1.InitConnection();
            while(!host.ContainsPlayer(player1.Name)) {}

            Assert.That(PirateScanner.CheckIp(PirateScanner.GetIp("127.0.0.1"), 4939, 1000));

            PirateClient player2 = new PirateClient("Player2", "127.0.0.1", 4939);
            player2.SetGame(new Game());
            player2.BetRequested += OnBetRequest;
            player2.CardRequested += OnCardRequest;
            player2.Disconnected += OnDisconnect;
            player2.InitConnection();
            while(!host.ContainsPlayer(player2.Name)) {}
            Assert.That(host.ContainsPlayer(player1.Name));

            var ps = new PirateScanner();
            var gameinfos = ps.ScanForGames(4939, 2000);
            Assert.That(gameinfos.Count > 0);
            var gameinfo = gameinfos[0];

            PirateClient player3 = new PirateClient("Player3", gameinfo.Ip, 4939);
            player3.SetGame(new Game());
            player3.BetRequested += OnBetRequest;
            player3.CardRequested += OnCardRequest;
            player3.Disconnected += OnDisconnect;
            player3.InitConnection();
            while(!host.ContainsPlayer(player3.Name)) {}
            Assert.That(player1.Name == host.PlayerFromSocket(host.GetPlayers().First().Socket).Name);

            while(host.Game.Players.Count != 3) {}

            host.StartGame();

            while(!host.Game.Finished) {
                Assert.That(host.Game.Started);
            }

            Assert.That(host.Game.Finished);

            host.Stop();

            while(player1.Socket.Connected || player2.Socket.Connected || player3.Socket.Connected) {}
        }
        /// <summary>
        /// Start game.
        /// </summary>
        /// <param name="host">The game.</param>
        public static void StartGame(PirateHost host)
        {
            Contract.Requires(host != null && host.PlayerCount >= 2);
            host.StopAccepting();

            var dealerIndex = CollectionFnc.PickRandom(0, host.Game.Players.Count - 1);
            Console.WriteLine("Starting player is: " + host.Game.Players[dealerIndex].Name);

            var msg = new PirateMessage(PirateMessageHead.Gstr, PirateMessage.ConstructStartingPlayer(host.Game.Players[dealerIndex]));
            foreach(var pclient in host.GetPlayers()) {
                host.SendMessage(pclient, msg);
            }

            host.Game.Start(true, dealerIndex);
        }
        /// <summary>
        /// Set player info.
        /// </summary>
        /// <param name="host">The host.</param>
        /// <param name="pclient">The client.</param>
        /// <param name="data">The data received from client.</param>
        public static void SetPlayerInfo(PirateHost host, PirateClient pclient, PirateMessage data)
        {
            Contract.Requires(host != null && pclient != null && data != null && data.Head == PirateMessageHead.Pnfo);
            var player = PirateMessage.GetPlayerName(data);
            if (player == null) return;

            if (!host.ContainsPlayer(player)) {
                if(host.Game.Contains(pclient)) host.Game.RemovePlayer(pclient);
                host.SetPlayerName(pclient, player);
                host.Game.ClearPlayers();
                host.Game.AddPlayers(host.GetPlayers());
                pclient.SetGame(host.Game);
                SendPlayerInfo(host);
            }else {
                var msg = new PirateMessage(PirateMessageHead.Erro, PirateError.NameAlreadyTaken.ToString());
                host.SendMessage(pclient, msg);
            }
        }
        /// <summary>
        /// Send player info.
        /// </summary>
        /// <param name="host">The host.</param>
        public static void SendPlayerInfo(PirateHost host)
        {
            Contract.Requires(host != null);

            var msg = new PirateMessage(PirateMessageHead.Pigm, PirateMessage.ConstructBody(host.GetPlayers().Select(player => player.ToString()).ToArray()));

            if (host.PlayerCount > 0) {
                if(host.DebugMode) Console.WriteLine("Host: Players in game:");
                foreach (var player in host.GetPlayers()) {
                    if(host.DebugMode) Console.WriteLine("\t" + player.Name);
                    host.SendMessage(player, msg);
                }
            }
        }
        /// <summary>
        /// Round finished.
        /// </summary>
        /// <param name="host">The host.</param>
        public static void RoundFinished(PirateHost host)
        {
            Contract.Requires(host != null);

            var body =
                PirateMessage.ConstructBody(
                    PirateMessage.ContstructPlayerScores(host.Game.GetRoundScoreTotal(host.Game.CurrentRound)));
            var msg = new PirateMessage(PirateMessageHead.Frnd, body);

            foreach (var player in host.GetPlayers()) {
                host.SendMessage(player, msg);
            }
        }
        /// <summary>
        /// Request bets from players.
        /// </summary>
        /// <param name="host">The host.</param>
        public static void RequestBets(PirateHost host)
        {
            Contract.Requires(host != null);

            var msg = new PirateMessage(PirateMessageHead.Breq, "");
            foreach(var pclient in host.GetPlayers()) {
                host.SendMessage(pclient, msg);
            }
        }
        /// <summary>
        /// Play card from player.
        /// </summary>
        /// <param name="host">The host.</param>
        /// <param name="data">Data received from client.</param>
        public static void PlayCard(PirateHost host, PirateMessage data)
        {
            Contract.Requires(host != null && data != null && data.Head == PirateMessageHead.Pcrd);
            var playerName = PirateMessage.GetPlayerName(data);
            var player = host.PlayerFromString(playerName);
            var card = Card.FromString(data.Body);

            if(!player.CardPlayable(card, host.Game.Round.BoardCards.FirstCard)) {
                ErrorMessage(host, player, PirateError.CardNotPlayable);
                var returnCard = new PirateMessage(PirateMessageHead.Xcrd, card.ToString());
                host.SendMessage(player, returnCard);
                RequestCard(host, player);
                return;
            }

            Console.WriteLine(player.Name + " plays " + card.ToShortString());

            var msg = new PirateMessage(
                PirateMessageHead.Pcrd, PirateMessage.ConstructBody(player.ToString(), card.ToString()));

            foreach(var pclient in host.GetPlayers()) {
                host.SendMessage(pclient, msg);
            }

            player.PlayCard(card);
            //host.Game.Round.PlayCard(player, card);
            if(!host.Game.Round.Finished) {
                RequestCard(host, host.PlayerFromIndex(host.Game.Round.CurrentPlayer));
            }else {
                host.Game.NewRound();
            }
        }
        /// <summary>
        /// New round.
        /// </summary>
        /// <param name="host">The host.</param>
        public static void NewRound(PirateHost host)
        {
            Contract.Requires(host != null);

            var body =
                PirateMessage.ConstructBody(
                    PirateMessage.ConstructDealer(host.Game.Players[host.Game.Round.Dealer].Name),
                    PirateMessage.ConstructRoundNumber(host.Game.CurrentRound));
            var msg = new PirateMessage(PirateMessageHead.Nrnd, body);
            foreach (var pclient in host.GetPlayers()) {
                host.SendMessage(pclient, msg);
            }
            Console.WriteLine("Starting new round: " + host.Game.CurrentRound);
        }
        /// <summary>
        /// New pile.
        /// </summary>
        /// <param name="host">The host.</param>
        public static void NewPile(PirateHost host)
        {
            Contract.Requires(host != null);

            var body = PirateMessage.ConstructBody(PirateMessage.ConstructPlayerTricks(host.Game.Round));
            body = PirateMessage.AppendBody(body, PirateMessage.ConstructWinner(host.Game.Round.LastTrick.Winner));
            var msg = new PirateMessage(PirateMessageHead.Trdn, body);

            foreach(var player in host.GetPlayers()) {
                host.SendMessage(player, msg);
            }
        }
        /// <summary>
        /// Game finished.
        /// </summary>
        /// <param name="host">The host.</param>
        public static void GameFinished(PirateHost host)
        {
            Contract.Requires(host != null);

            var body =
                PirateMessage.ConstructBody(
                    PirateMessage.ContstructPlayerScores(host.Game.GetTotalScores()));
            body = PirateMessage.AppendBody(body, PirateMessage.ConstructWinner(host.Game.Leader));
            var msg = new PirateMessage(PirateMessageHead.Gfin, body);

            foreach (var player in host.GetPlayers()) {
                host.SendMessage(player, msg);
            }
        }