Use a mutex to protect the count of open sockets.