Change handling of client close. Closes #2096. Closes #1686