Refactored the code to be less asynchronous and avoid running out of
sockets.