void CConnectivityAgent::threadFunc()
{
   LOG4CPLUS_TRACE_METHOD(logger, __PRETTY_FUNCTION__);

   while (false == getStopFlag())
   {
      mMainFuncMutex.lock();

      if (!mpManager || !mConnectionEstablished)
      {
         for (tFinders::iterator iter = mFinders.begin(); iter != mFinders.end(); ++iter)
         {
            (*iter)->startSearch();
         }
      }
      else
      {
         CCarrierAdapter* pCarrier = getCurrentCarrierAdapter();

         if (!pCarrier || pCarrier->isBroken())
         {
            LOG4CPLUS_WARN(logger, "agent: connection lost");
            mConnectionEstablished = false;
            mGenderType = eAnyGender;

            if (this->mpManager)
            {
               LOG4CPLUS_INFO(logger, "agent: calling mpManager->OnDisconnected ");
               mpManager->OnDisconnected();
            }
         }
      }
      mMainFuncMutex.unlock();

      CThread::sleep(2000);
   }
   LOG4CPLUS_INFO(logger, "agent: stopped");
}
void CTcpAutoConnectionFinder::performSearch()
{
   LOG4CPLUS_TRACE_METHOD(msLogger, __PRETTY_FUNCTION__);

   const int try_count = 10;

   std::string cli_addr;

   mTurnRandomNumber = random() % std::numeric_limits<UInt8>::max();

   LOG4CPLUS_INFO(msLogger, "CTcpAutoConnectionFinder::performSearch() random number = " + std::string(convertIntegerToString(mTurnRandomNumber)));

   mBrdMsg.random = mTurnRandomNumber;

   if (!checkInterfaces())
   {
      return;
   }

   switch (waitForMessage(cli_addr))
   {
   case eSERVER:
      {
         CCarrierAdapter* pSrvAdapter = createServerCarrierAdapter();

         bool connected = false;
         for (int i = 0; i < try_count && pSrvAdapter; ++i)
         {
            if (!pSrvAdapter->isBroken())
            {
               setReady(pSrvAdapter);
               pSrvAdapter = NULL;
               connected = true;
               break;
            }
            else
            {
               this->sleep(FINDER_SLEEP_BETWEEN_RETRIES_MS);
            }
         }

         if (!connected)
         {
            LOG4CPLUS_INFO(msLogger, "CTcpAutoConnectionFinder::performSearch() connection failed, removing server");
            delete pSrvAdapter;
         }
      }
      break;
   case eCLIENT:
      {
         for (int i = 0; i < try_count; ++i)
         {
            if (createClientCarrierAdapter(cli_addr))
            {
               break;
            }
            else
            {
               this->sleep(FINDER_SLEEP_BETWEEN_RETRIES_MS);
            }
         }
      }
      break;
   case eERROR:
      break;
   } // switch

}