port of send_error_listen from crsmithdev's personal repo