// Update is called once per frame
    void Update()
    {
        if (send)
        {
            send = false;
            SendMsg(msg);
        }

        if (state == ClientState.JOINING)
        {
            connectionTimeOutTimer += Time.deltaTime;

            if (connectionTimeOutTimer > connectionTimeOut)
            {
                state = ClientState.DISCONNECTING;
                Debug.LogWarning("Client could not connect to server");
                EnableSafeToDisconnect();
                playerController.OnFailedToConnect();
            }
        }

        if (state == ClientState.JOIN_ACCEPTED)
        {
            state = ClientState.JOINED;
            gr.Reset(playerController.playerID);
            playerController.OnConnected();
        }

        if (state == ClientState.BEGIN_ANIMATION)
        {
            gr.SetCommands(newGameState.cmds);
            state = ClientState.ANIMATION;
        }

        if (state == ClientState.ANIMATION)
        {
            timeSinceLastAnim += Time.deltaTime;
            if (timeSinceLastAnim > targetAnimDelay)
            {
                timeSinceLastAnim = 0.0f;
                if (gr.StepThroughSim())
                {
                    gr.SetGameState(newGameState);
                    Debug.Log("Turn: " + gr.GetTurn());

                    // Check if first turn
                    if (gr.GetTurn() == 0)
                    {
                        playerController.LookAtOwnShip();
                    }

                    state = ClientState.COMMAND;

                    if (gr.IsGameFinished())
                    {
                        // END GAME
                        playerController.uiController.EnableWinText(true, gr.GetWinner() == playerController.playerID);

                        //SendDisconnect();
                    }
                    else
                    {
                        playerController.SetTurnTimer(newGameState.turnTimer);
                        playerController.NewTurn(gr.GetShipControllers());
                    }
                }
            }
        }

        if (state == ClientState.DISCONNECTING)
        {
            if (safeToDisconnet)
            {
                Disconnect();
                DisableSafeToDisconnect();
            }
        }
    }