Improve debug logging in listen().
Add support for testing using valgrind.