How to prevent StreamReader from hanging when reading from a socket

I’m trying to implement an IRC client in Unity and I’m hanging when reading from a socket stream. The relevant code looks like this:

sock.Connect(server,port);
		if (!sock.Connected)
		{
			Debug.Log("Failed to connect!");
			Application.Quit();
		}
		socketStream=sock.GetStream();

		input = new System.IO.StreamReader(socketStream);
		output = new System.IO.StreamWriter(socketStream);		

And in update I have Debug.Log(input.ReadLine());

When the the stream runs out the application hangs. Google showed other people had this problem but I didn’t find a solution. There are IRC packages available on the asset store so it should be possible.

That’s just how sockets work. If you request to read data, but there is none there, it thinks you’re waiting on some and will hang until it gets it.

What you need to do is either poll the stream to check for data before reading, or run your networking code on a synchronous thread.