Fixed bug that could cause connection to close prematurely.