Fix a race manipulating file descriptors in the test suite.  Always close the connection before closing redirected stdout and stderr.  Make sure the connection and redirected files are both closed if a test fails.