Fixes #505 - Make sure all output is send on the network before closing