Add a test for socket closing

This is a failing test which exposes the problem in #344, I believe.