safely stops threads in Server and Client