Close threads and sockets before closing

Still needs some improvements, but the status thread and the 2 sockets
are closed before exiting the application