improvements to stream/socket handling, but still buggy