Correctly close sockets on shutdown