To force the listening thread terminate in a safe way I made the boolean m_bIsShuttingDownflag known to both main and listening thread. In addition, my main thread knows the port m_sLocalPort...