Adapted by Steve Underwood from the source code supplied in RFC3???.