/** Keeps server running until there are no players for longer than the set timeout */
  public void run() {
    boolean keepRunning = true;

    while (keepRunning) {
      // check that game still has players
      if (getNumberOfPlayers() < 1) {
        Date initialTime = new Date();
        while (getNumberOfPlayers() < 1) {
          Date currentTime = new Date();
          Long timeDiff = currentTime.getTime() - initialTime.getTime();
          if (GAME_TIMEOUT < timeDiff) {
            keepRunning = false;
          }
        }
      } else {
        boolean playersReady;
        // check if players are ready for question
        do {
          playersReady = true;

          try {
            for (GameServerWorker player : players) {
              if (!player.getReadyForQuestion()) {
                playersReady = false;
              }
            }
          } catch (ConcurrentModificationException e) {
            playersReady = false;
          }

          try {
            Thread.sleep(1000);
          } catch (InterruptedException e) {
            e.printStackTrace();
          }

        } while (!playersReady);
        handleNextQuestion();
      }
    }
    // remove this game from the server
    gameServer.removeGameInstance(portNumber);
  }
  /**
   * Handles sending question to the multicast group and notifying them that all players are ready.
   */
  public void handleNextQuestion() {
    sendDataPacket("SENDPLAYERDATA");
    // create datagram packet
    sendDataPacket("QUESTION: " + getNewQuestion());

    boolean allPlayersReady;
    // check if all players received question
    do {
      allPlayersReady = true;
      for (GameServerWorker player : players) {
        if (!player.getReadyToAnswer()) {
          allPlayersReady = false;
        }
      }

      try {
        Thread.sleep(1000);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
    } while (!allPlayersReady);

    sendDataPacket("ANSWERQUESTION");
  }