Adapted by Steve Underwood <steveu@coppice.org> from the source code supplied in RFC3???.