If this is for a serious application, use
LWP. It does these things for you. If the purpose of this code is to teach yourself socket programming, that's a different issue. The code looks basically ok, except that I wouldn't bother putting it through the HTML parsing loop unless you've actually confirmed that the Content-type is 'text/html'. The reason is that you're doing unnecessary work, and you're also splitting on newlines, which is a strange thing to do when reading binary data. If you would like to know how to download a file from HTTP yourself look at the source code for
LWP::Protocol and
LWP::Protocol::http. This helped me recently in fact with a similar issue.