improved encodeHeader function and decodeHeader handling to follow RFC's