changed API to support more options
supporting headers and witing out to an IO stream