short LobbyGame::HandleJoinGameRequest(ReadBuffer &theMsg, LobbyClient *theClient, WriteBuffer &theReply)
{
	LobbyPlayerPtr aPlayer;

	if(IsGameFull())
		return LobbyGameStatus_GameFull;
	else if(mInProgress)
		return LobbyGameStatus_GameInProgress;

	LobbyConfig *aConfig = LobbyConfig::GetLobbyConfig();
	if(aConfig!=NULL && !aConfig->mAllowDuplicateNames)
	{
		// check if the name is already used
		LobbyClientMap::const_iterator aClientItr = mClientList->GetClientMap().begin();
		for(; aClientItr != mClientList->GetClientMap().end(); ++aClientItr)
		{
			if (aClientItr->second->GetName() == theClient->GetName() && aClientItr->second.get() != theClient)
				return LobbyGameStatus_DuplicateName;
		}
	}
	


	aPlayer = LobbyPlayer::CreatePlayer();
	theClient->SetPlayer(aPlayer);

	try
	{
		unsigned short aPing = theMsg.ReadShort();	
		aPlayer->SetPing(aPing);

		short aStatus = HandleJoinGameRequestHook(theMsg, theClient);
		if(aStatus!=LobbyGameStatus_Success)
		{
			theClient->SetPlayer(NULL);
			return aStatus;
		}
	}
	catch(ReadBufferException&)
	{
		theClient->SetPlayer(NULL);
		return LobbyGameStatus_UnpackFailure;
	}


	// Send PlayerList back
	mNumPlayers++;	
	theReply.AppendShort(mNumPlayers); 

	LobbyPlayerList aList(mClientList);	
	while(aList.HasMorePlayers())
	{
		LobbyClient *aClient;
		LobbyPlayer *aPlayer = aList.GetNextPlayer(&aClient);
		if(aPlayer!=NULL)
		{
			theReply.AppendShort(aClient->GetClientId());
			aPlayer->WriteData(theReply);
		}
	}
	
	GetJoinGameReplyHook(theClient,theReply);
	return LobbyGameStatus_Success;

}