Port of https://github.com/cfortune/PHP-Bounce-Handler