Refactor body of connection_dir_client_reached_eof()

What's this 630-line function doing in our codebase?