modify error handling to return status codes and retry-after info

The original code by Alex Lockwood has been changed in a number of ways:
 - Redundant code removed.
 - The error return type has been extended to include http status codes
   as well as the retry-after header info.
 - New function to parse the retry-after header and convert it to a
   delta rather than a dateTime - which could be problematic if clients
   have an incorrect time set (yes, it happens).
 - max function added to simplify code.