private void advanceState()
 {
     if (state == 0)
     {
         for (int i = 0; i < maxPlayers; i++)
         {
             if (players[i] != null && players[i].leftGame)
             {
                 players[i] = null;
                 numPlayers--;
             }
         }
         numActivePlayers = 0;
         numAllIn = 0;
         deck.shuffle();
         for (int i = 0; i < maxPlayers; i++)
         {
             if (players[i] != null)
             {
                 if(players[i].setUpNewGame(deck.drawNext(), deck.drawNext()))
                 {
                     numActivePlayers++;
                 }
                 else
                 {
                     connectedPlayers--;
                     numPlayers--;
                     ServerSocket.Send(players[i].mySocket, "Kick\n<EOF>");
                     ServerSocket.Send(players[i].mySocket, ServerSocket.printServers());
                     ServerSocket.Send(players[i].mySocket, "StoredChips\n" + ServerSocket.database.getNumberOfChips(players[i].name) + "\n<EOF>");
                     players[i] = null;
                 }
             }
         }
         if (numActivePlayers <= 1)
         {
             BroadcastPublicInfo();
             return;
         }
         dealtCards = new Card[5];
         dealer = advancePlayer(dealer);
         players[dealer].status = "Dealer";
         currentPlayer = advancePlayer(dealer);
         if(players[currentPlayer].bet(smallBlind, "Small Blind", smallBlind))numAllIn++;
         players[currentPlayer].hasActed = false;
         currentPlayer = advancePlayer(currentPlayer);
         if (players[currentPlayer].bet(smallBlind * 2, "Big Blind", smallBlind * 2)) numAllIn++;
         players[currentPlayer].hasActed = false;
         currentPlayer = advancePlayer(currentPlayer);
         players[currentPlayer].status = "Waiting";
         BroadcastPublicInfo();
         SendPrivateInfo();
         ServerSocket.Send(players[currentPlayer].mySocket, players[currentPlayer].printCall(getMaxBet()));
         state = 1;
     }
     else if (state == 1)
     {
         for (int j = 0; j < 3; j++)
         {
             dealtCards[j] = deck.drawNext();
         }
         state = 2;
         if(numAllIn + 1 >= numActivePlayers)
         {
             advanceState();
             return;
         }
         for (int i = 0; i < maxPlayers; i++)
         {
             if (players[i] != null)
             {
                 players[i].setUpNewBet();
             }
         }
         currentPlayer = advancePlayer(dealer);
         players[currentPlayer].status = "Waiting";
         BroadcastPublicInfo();
         SendPrivateInfo();
         ServerSocket.Send(players[currentPlayer].mySocket, players[currentPlayer].printCall(getMaxBet()));
     }
     else if (state == 2)
     {
         state = 3;
         dealtCards[3] = deck.drawNext();
         if (numAllIn + 1 >= numActivePlayers)
         {
             advanceState();
             return;
         }
         for (int i = 0; i < maxPlayers; i++)
         {
             if (players[i] != null)
             {
                 players[i].setUpNewBet();
             }
         }
         currentPlayer = advancePlayer(dealer);
         players[currentPlayer].status = "Waiting";
         BroadcastPublicInfo();
         SendPrivateInfo();
         ServerSocket.Send(players[currentPlayer].mySocket, players[currentPlayer].printCall(getMaxBet()));
     }
     else if (state == 3)
     {
         dealtCards[4] = deck.drawNext();
         state = 4;
         if (numAllIn + 1 >= numActivePlayers)
         {
             advanceState();
             return;
         }
         for (int i = 0; i < maxPlayers; i++)
         {
             if (players[i] != null)
             {
                 players[i].setUpNewBet();
             }
         }
         currentPlayer = advancePlayer(dealer);
         players[currentPlayer].status = "Waiting";
         BroadcastPublicInfo();
         SendPrivateInfo();
         ServerSocket.Send(players[currentPlayer].mySocket, players[currentPlayer].printCall(getMaxBet()));
     }
     else if (state == 4)
     {
         currentPlayer = -1;
         numActivePlayers = 0;
         Hand[] bestHands = new Hand[maxPlayers];
         for (int i = 0; i < maxPlayers; i++)
         {
             if (players[i] != null && !players[i].isFolded)
             {
                 bestHands[i] = players[i].getBestHand(dealtCards);
                 players[i].status = bestHands[i].print();
                 players[i].displayCards = true;
             }
         }
         int newLeftovers = 0;
         while(getPot() > 0)
         {
             Hand bestHand = null;
             bool[] isBest = new bool[maxPlayers];
             int bestCount = 0;
             for (int i = 0; i < maxPlayers; i++)
             {
                 if(bestHands[i] != null)
                 {
                     if(bestHand == null || bestHands[i].isBetter(bestHand) > 0)
                     {
                         bestHand = bestHands[i];
                         isBest = new bool[maxPlayers];
                         isBest[i] = true;
                         bestCount = 1;
                     }
                     else if(bestHands[i].isBetter(bestHand) == 0)
                     {
                         isBest[i] = true;
                         bestCount++;
                     }
                 }
             }
             int lowBet = int.MaxValue;
             for (int i = 0; i < maxPlayers; i++)
             {
                 if(isBest[i])
                 {
                     lowBet = Math.Min(lowBet, players[i].inPot);
                 }
             }
             int totalPot = leftoverPot;
             leftoverPot = 0;
             for (int i = 0; i < maxPlayers; i++)
             {
                 if(players[i] != null)
                 {
                     totalPot += players[i].payout(lowBet);
                 }
             }
             int finalPayout = totalPot/bestCount;
             for (int i = 0; i < maxPlayers; i++)
             {
                 if (isBest[i])
                 {
                     players[i].chips += finalPayout;
                     totalPot -= finalPayout;
                     bestHands[i] = null;
                 }
             }
             newLeftovers += totalPot;
         }
         leftoverPot = newLeftovers;
         BroadcastPublicInfo();
         SendPrivateInfo();
         state = 5;
         advanceState();
     }
     else if (state == 5)
     {
         currentPlayer = -1;
         state = 0;
         Task.Factory.StartNew(() =>
         {
             Thread.Sleep(10000);
             advanceState();
         });
     }
 }
 public int isBetter(Hand h)
 {
     for (int i = 0; i < testVals.Length; i++)
     {
         if (testVals[i] > h.testVals[i]) return 1;
         if (testVals[i] < h.testVals[i]) return -1;
     }
     return 0;
 }
 public Hand getBestHand(Card[] c)
 {
     Hand bestHand = null;
     Card[] allCards = new Card[7];
     allCards[0] = card1;
     allCards[1] = card2;
     for(int i = 0; i < 5; i ++)
     {
         allCards[i + 2] = c[i];
     }
     for (int i = 0; i < 7; i++)
     {
         for (int j = 0; j < i; j++)
         {
             Card[] handCards = new Card[5];
             int ix = 0;
             for (int k = 0; k < 7; k++)
             {
                 if (k != i && k != j)
                 {
                     handCards[ix] = allCards[k];
                     ix++;
                 }
             }
             Hand newHand = new Hand(handCards);
             if (bestHand == null || newHand.isBetter(bestHand) > 0)
             {
                 bestHand = newHand;
             }
         }
     }
     return bestHand;
 }