Handle closed connection (EOF) properly