Added a more generic output stream handling

Previous implementation didn't work for Duplex streams.