 <p>Look carefully at this line of code:</p> <pre><code>s.send("".join(["NICK",NICK,"\r\n"]).encode()) </code></pre> <p>If you replaced <code>s.send</code> with <code>print</code>, you’d realize it was sending strings like this:</p> <pre><code>NICKnick&lt;CR&gt;&lt;LF&gt; </code></pre> <p>There’s no space! That makes it an invalid command, and makes registration fail. At some point, the server gives up on receiving a valid registration from you, and so sends you an error and closes the connection. So make sure you include a space:</p> <pre><code>s.send("".join(["NICK ",NICK,"\r\n"]).encode()) </code></pre> <p>At least then you’d be sending a valid registration.</p>