// Resume the game by restarting the timer and send the restart message to the opponent
        private int resumeGame(bool sendRequestToOverPlayer)
        {
            // If it is a 2 player game we need to resume the other game if it is not it which resume us
            if(sendRequestToOverPlayer)
            {
                byte[] message = {Constants.IdMessageResume};
                Network.Instance.CommunicationWay.Write(message);
            }
            else
            {
                // Restart the timer if it is the opponeny who send the message to restart
                // Else we are going to wait that he receives the message
                DialogActivity.CloseAll();

                _gameTimer.AutoReset = true;
                _gameTimer.Interval = getTimerLapse();
                _gameTimer.Start();

                _originPause = StopOrigin.None;
            }

            return 0;
        }
        private int OnLostConnection(byte[] message)
        {
            #if DEBUG
            Log.Debug(Tag, "OnLostConnection");
            #endif

            DialogActivity.CloseAll();

            if(_gameState == GameState.Play)
            {
                // If we lost the connection, we stop the game display a pop-up and try to reconnect
                Network.Instance.ConnectionLostEvent -= OnLostConnection;
                _gameTimer.Stop();
                _originPause = StopOrigin.LostConnection;

                ReconnectActivity._messageFail = message;
                var serverIntent = new Intent(this, typeof(ReconnectActivity));
                StartActivityForResult(serverIntent,(int) Utils.RequestCode.RequestReconnect);
            }
            else
            {
                /*
                switch(_gameState)
                {
                case GameState.GameOverWin:
                    intent = DialogActivity.CreateYesDialog(this, Resource.String.ConnectionLost, Resource.String.cannotRestartGameWin, Resource.String.ok, delegate {Finish();});
                    break;
                case GameState.GameOverLost:
                    intent = DialogActivity.CreateYesDialog(this, Resource.String.ConnectionLost, Resource.String.cannotRestartGameLost, Resource.String.ok, delegate {Finish();});
                    break;
                case GameState.OpponentReadyForRestart:
                case GameState.WaitingForRestart: // display of the same pop-up in these case
                    intent = DialogActivity.CreateYesDialog(this, Resource.String.ConnectionLost, Resource.String.lostFriend, Resource.String.ok, delegate {Finish();});
                    break;
                }*/

                Intent intent = DialogActivity.CreateYesDialog(this, Resource.String.ConnectionLost, Resource.String.lostFriend, Resource.String.ok, delegate {Finish();});
                SetResult(Result.Ok);
                StartActivity(intent);
            }

            return 0;
        }
 protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
 {
     #if DEBUG
     Log.Debug(Tag, "onActivityResult, resultCode=" + resultCode);
     #endif
     if(requestCode == (int) Utils.RequestCode.RequestReconnect)
     {
         if(resultCode == Result.Ok)
         {
             Network.Instance.ConnectionLostEvent += OnLostConnection;
             // We can restart the game if the connection is working again
             _gameTimer.Start();
             _originPause = StopOrigin.None;
         }
         else
         {
             // We didn't reconnect to the other device so we end the party
             SetResult(Result.Ok);
             Finish();
         }
     }
 }
        // Pause the game, display a pop-up and send a message to the remote device if asked
        protected override int pauseGame(bool requestFromUser)
        {
            _gameTimer.Stop();

            // Set the origin so it is the right player who restart the game
            _originPause = requestFromUser ? StopOrigin.MyPause : StopOrigin.PauseOpponent;

            // We need to pause the other game if it is not it which stop us
            string name = null;
            if(requestFromUser)
            {
                byte[] message = {Constants.IdMessagePause};
                Network.Instance.CommunicationWay.Write(message);
            }
            else
            {
                name = _player2._name;
            }

            Intent intent = UtilsDialog.CreatePauseGameDialog(this, name);
            StartActivity(intent);

            return 0;
        }
        public int WriteMessageEventReceived(byte[] writeBuf)
        {
            if(writeBuf[0] == Constants.IdMessageResume)
            {
                _gameTimer.AutoReset = true;
                _gameTimer.Interval = getTimerLapse();
                _gameTimer.Start();

                _originPause = StopOrigin.None;
            }
            else if(writeBuf[0] == Constants.IdMessageNewGame)
            {
                lock (_stateLocker)
                {
                    if(_restartState == Network.StartState.OPPONENT_READY)
                    {
                        SetResult(Result.FirstUser);
                        Finish();
                    }
                    else if(_restartState == Network.StartState.NONE)
                    {
                        _restartState = Network.StartState.WAITING_FOR_OPPONENT;
                    }
                }
            }
            return 0;
        }