refactored response writing to better handle raw bytes and encoded strings.