Improved previous commit to handle DuplexStreams as well