Shut down gracefully, close socket and db