explicitly close the socket rather than rely on implicit

This addresses the main issue in #519