The IRC protocol is asynchronous and there is no guarantee when you will actually receive a response from the server and you will also receive data from the connection without having sent anything yourself before.
It is quite natural that your code never leaves the While-Loop; the server does not disconnect your client or close the connection.
Instead, it will send "PING!" requests to your client which you should respond to with sending "PONG!" answers. If you don't do that, the server will run into a timeout and -then- disconnect. If you wait long enough, this also happens with your code.
It basically comes down to that you have to adept your code to the asynchronous nature of the protocol.
It is quite natural that your code never leaves the While-Loop; the server does not disconnect your client or close the connection.
Instead, it will send "PING!" requests to your client which you should respond to with sending "PONG!" answers. If you don't do that, the server will run into a timeout and -then- disconnect. If you wait long enough, this also happens with your code.
It basically comes down to that you have to adept your code to the asynchronous nature of the protocol.