Refactored marshalHostAndPort

- Added accumulator object
- Composed into multiple methods
Refactored marshalHeaders

- Removed nested conditional, and moved it up a level to catch earlier
CS fixes (trailing whitespace)
Merge branch 'hotfix/refactor-host-port'

More scrutinizer-suggested refactorings:
- added an accumulator to marshalHostAndPort(), and extracted several additional
  methods from it to simplify the workflow.
- updated marshalHeaders() to remove a nested conditional; the nested
  conditional could be promoted up to simplify the workflow.