Detect connection closed by remote host

Thanks to jamescrosswell for contributing this!