private void ResetClientSideGameState()
        {
            // We don't have a player number right now.
            this.playerNumber = (GamePlayer)(-1);

            this.stateCode = ClientStateCode.WaitingForConnection;
        }
        private void EndGame(GameOverCondition condition)
        {
            if (this.stateCode != ClientStateCode.GameIsOver)
            {
                ClientStateCode lastStateCode = this.stateCode;
                this.stateCode = ClientStateCode.GameIsOver;

                if (lastStateCode > 0)
                {
                    this.Display("Disconnected.");

                    this.client.Disconnect();

                    if (lastStateCode == ClientStateCode.GameIsInProgress)
                        lock (this.locker)
                        {
                            this.gameClientLogic.EndGame(this.playerNumber, condition);
                            this.gameStateQueue.Enqueue(this.gameClientLogic.GetGameState());
                        }
                }

                ChangeConnectButtonText("Connect");
            }

            pictureBox1.Invalidate();
        }
        private void NewMessageCallbackHandler(string msg)
        {
            // New data received.
            this.Display("> " + msg);

            if (GameMessage.IsVersion(msg))
            {
                // Version Info.
                GameMessage.Version versionMsg = new GameMessage.Version(msg);

                if (this.stateCode == ClientStateCode.WaitingForVersionMsg)
                {
                    this.stateCode = ClientStateCode.WaitingForYourPlayerNumberMsg;

                    this.Display("Received game manager Version message.");

                    this.Display("Version: " + versionMsg.ManagerVersion + " Game: '" + versionMsg.GameName + "' v" + versionMsg.GameVersion + " conf: '" + versionMsg.GameConfig + "'");

                    if (versionMsg.GameName.Equals("Owari"))
                        this.gameClient = new OwariClientInterface();
                    else if (versionMsg.GameName.Equals("Tzaar"))
                        this.gameClient = new TzaarClientInterface();
                    else
                    {
                        this.Display("The game is not supported by this client!");
                        return;
                    }

                    this.initVersionMsg = versionMsg;
                }
                else if (this.stateCode == ClientStateCode.WaitingForYourPlayerNumberMsg || this.stateCode == ClientStateCode.WaitingForBoardStateMsg || this.stateCode == ClientStateCode.WaitingForVersionMsg)
                {
                    this.Display("(0) I did not receive the correct initialization sequence!");
                    ResetClientSideGameState();
                }
                else
                {
                    this.Display("Ignoring unexpected Version message: " + this.initVersionMsg);
                }
            }
            else if (GameMessage.IsChat(msg))
            {
                // Chat message.
                GameMessage.Chat chatMsg = new GameMessage.Chat(msg);

                this.Display("Your Opponent Says: " + msg);
            }
            else if (GameMessage.IsBoardState(msg))
            {
                if (this.stateCode == ClientStateCode.WaitingForBoardStateMsg)
                {
                    this.stateCode = ClientStateCode.GameIsInProgress;

                    this.initBoardStateMsg = this.gameClient.GetNewBoardStateGameMessage(msg);

                    this.gameStateQueue.Clear();

                    this.gameClientLogic = this.gameClient.GetNewGameClientLogic(this.pictureBox1, this.initVersionMsg, this.playerNumber, this.initBoardStateMsg.Board);

                    this.gameStateQueue.Enqueue(this.gameClientLogic.GetGameState());
                }
                else if (InInitPhase())
                {
                    this.Display("Received BoardState out of sequence!");
                    ResetClientSideGameState();
                }
                else
                {
                    this.Display("Ignoring unexpected BoardState message.");
                }
            }
            else if (GameMessage.IsYourPlayerNumber(msg))
            {
                if (this.stateCode == ClientStateCode.WaitingForYourPlayerNumberMsg)
                {
                    this.stateCode = ClientStateCode.WaitingForBoardStateMsg;

                    this.playerNumber = new GameMessage.YourPlayerNumber(msg).PlayerNumber;
                }
                else if (InInitPhase())
                {
                    this.Display("Received YourPlayerNumber out of sequence!");
                    ResetClientSideGameState();
                }
                else
                {
                    this.Display("Ignoring unexpected YourPlayerNumber message.");
                }
            }
            else if (GameMessage.IsYourTurn(msg))
            {
                GameMessage.YourTurn yourTurnMsg = new GameMessage.YourTurn(msg);
                this.Display("Received YourTurn message.");
            }
            else if (GameMessage.IsMove(msg))
            {
                if (this.stateCode == ClientStateCode.GameIsInProgress)
                {
                    GameMessage.Move newMove = this.gameClient.GetNewMoveGameMessage(msg);

                    RecordMove(this.playerNumber, newMove);
                }
                else
                {
                    this.Display("Ignoring unexpected Move message.");
                }
            }
            else if (GameMessage.IsGameOver(msg))
            {
                if (this.stateCode == ClientStateCode.GameIsInProgress)
                {
                    GameMessage.GameOver gameOverMsg = new GameMessage.GameOver(msg);

                    EndGame(gameOverMsg.Condition);
                }
                else
                {
                    this.Display("Ignoring unexpected GameOver message.");
                }
            }
            else
            {
                this.Display("Unrecognized command: " + msg);
            }

            this.pictureBox1.Invalidate();
        }
        private void Connect()
        {
            if (this.stateCode != ClientStateCode.WaitingForConnection)
                return;

            IPAddress host;
            try
            {
                // Parse the contents of the server address box.
                host = IPAddress.Parse(this.textBox_IPAddress.Text);
            }
            catch (Exception ex)
            {
                // Parsing the server address failed.
                this.Display(ex.Message + "Please enter a valid IP address.");
                return;
            }

            int port = (int)this.numericUpDown_Port.Value;

            try
            {
                this.client.Connect(host, port);
            }
            catch (Exception ex)
            {
                this.Display(ex.Message + "Failed to connect.");
                return;
            }

            this.Display("Attempting to connect to " + host + " on port " + port + ".");

            ChangeConnectButtonText("Disconnect");

            ResetClientSideGameState();

            this.stateCode = ClientStateCode.WaitingForVersionMsg;

            this.pictureBox1.Invalidate();
        }