Shutdown channel factory if we created it
Fix test race condition

When finding an available port by binding a temporary ServerSocket to port 0, and then later using that port, the port might have been consumed by another test. Instead, tests should just bind the nifty server directly to port 0 and get the actual port it was bound to using getServerChannel after starting the server, and looking up the actual locally bound address.