Added missing mutex on listening and writing threads