The reason your code doesn’t work is because you’re not properly parsing from IRC. If you are not experienced in programming or the RFC then use a pre-existing library rather than attempting to create your own.
Your solution is hardcoded to check for words in specific response lines, coupled with predictions on the maximum number of lines you might receive until you find what you’re looking for. I suspect that these statements are the cause of your troubles.
I’d suggest making a single loop that checks for data on the socket, and properly parsing said data in correspondence with the IRC RFC.