void MessageDispatch::registerSessionlessDispatchClient(uint32 accountId)
{
    // Verify not allready registred.
    AccountClientMap::iterator iter = mAccountClientMap.find(accountId);

    if (iter == mAccountClientMap.end())
    {
        DispatchClient* dispatchClient = new DispatchClient();
        dispatchClient->setAccountId(accountId);
        mAccountClientMap.insert(std::make_pair(accountId,dispatchClient));
    }
}
void MessageDispatch::handleSessionMessage(NetworkClient* client, Message* message)
{

    DispatchClient* dispatchClient = 0;
    bool deleteClient = false;

    //boost::recursive_mutex::scoped_lock lk(mSessionMutex);

    message->ResetIndex();

    // What kind of message is it?
    uint32 opcode;
    message->getUint32(opcode);

    // We want to intercept the opClusterClientConnect and opClusterClientDisconnect messages
    // so we can create account specific clients for use in async calls.
    if (opcode == opClusterClientConnect)
    {
        dispatchClient = new DispatchClient();
        dispatchClient->setAccountId(message->getAccountId());
        dispatchClient->setSession(client->getSession());

        mAccountClientMap.insert(std::make_pair(message->getAccountId(),dispatchClient));
    }
    else if (opcode == opClusterClientDisconnect)
    {
        // First find our DispatchClient.
        AccountClientMap::iterator iter = mAccountClientMap.find(message->getAccountId());

        if(iter != mAccountClientMap.end())
        {
            dispatchClient = (*iter).second;
            mAccountClientMap.erase(iter);

            DLOG(INFO) << "Destroying Dispatch Client for account " << message->getAccountId();

            // Mark it for deletion
            deleteClient = true;

        }
        else
        {
            LOG(INFO) << "Could not find DispatchClient for account " <<  message->getAccountId() << " to be deleted.";

            client->getSession()->DestroyIncomingMessage(message);
            //lk.unlock();

            return;
        }
    }
    else
    {
        AccountClientMap::iterator iter = mAccountClientMap.find(message->getAccountId());

        if(iter != mAccountClientMap.end())
        {
            dispatchClient = (*iter).second;
        }
        else
        {
            client->getSession()->DestroyIncomingMessage(message);

            //lk.unlock();
            return;
        }
        /*
        else
        {
          dispatchClient = new DispatchClient();
          dispatchClient->setAccountId(message->getAccountId());
          dispatchClient->setSession(client->getSession());
          mAccountClientMap.insert(message->getAccountId(), dispatchClient);
        }
        */
    }
    //lk.unlock();

    MessageCallbackMap::iterator iter = mMessageCallbackMap.find(opcode);

    if(iter != mMessageCallbackMap.end())
    {
        // Reset our message index to just after the opcode.
        message->setIndex(4);

        // Call our handler
        (*iter).second(message, dispatchClient);
    }
    else
    {
        LOG(INFO) <<  "Unhandled opcode in MessageDispatch - " << opcode ;
    }


    // Delete the client here if we got a disconnect.
    if(deleteClient)
    {
        // We will delete the client when we delete the player or reconnect again.
        //delete dispatchClient;
        dispatchClient = NULL;
    }

    // We need to destroy the incoming message for the session here
    // We want the application to decide whether the message is needed further or not.
    // This is mainly used in the ConnectionServer since routing messages need a longer life than normal
    message->setPendingDelete(true);
}