Shutdown old server via command at startup

Also extract server code into it's own file.