Fix infinite read loop on closed TCP socket