Change to use NSInputStream and fix concurrency bug