in reply to POP3 Mail Client using IO::Socket module only

I am close to getting all of the messages in the mailbox listed with their message number, who the message is from and the subject but it is messing up and giving me ALL the header information of the message even though I only want the message number defined in the program as well as the "From" and "Subject" fields. It also seems to be only displaying the header info for one of the messages which means something is faulty with the "for" loop that I created that should work because someone else showed me how to arrange that. Here is my updated code for Option 1 "List Messages":
if ($choice =~ /^1$/) { print $socket "STAT",CRLF; $answer = <$socket>; print "$answer\n\n"; if ($answer =~ /^\+OK\s(\d{1,}).*/) { $msgcount = $1; print "You have " . $msgcount . " messages"; } if ($msgcount > 0) { for (my $i = 0; $i < $msgcount; $i++) { print $socket "STAT",CRLF; $answer = <$socket>; print "$answer\n\n"; $msgnum = $i; print "Message Number: " . $msgnum; print $socket "RETR " . $msgnum,CRLF; $answer = <$socket>; if ($answer =~ /^\-ERR/) { print "ERROR"; } else { # these are regular expressions to parse # out and display only the From and Subject # fields from the message header # information which I get using the RFC # 1939 protocol and I backreference the # expressions with "$1" and "$2" scalar # variables if ( $answer =~ /^From:(.*)/ ) { print "From: $1\n"; } if ( $answer =~ /^Subject:(.*)/ ) { print "Subject: $2\n"; } } print "\n"; } } else { print "\n\nYou currently have no messages."; <STDIN>; next; } next; }
If anyone has any suggestions for how to fix this or attempt to clean it up, it would be greatly appreciated. I DO NOT expect people to start writing the code for me. I just want a few snippets here and there of things that any of you think I may be missing or things that I need to include. Once, I get one part working with the regular expressions, I can easily get the others going because Option 2 is similar to Option 1 except I parse out the body of the message using a regular expression and Option 3 is just displaying both header and body which is everything I parsed out in Option 1 for the header (message number, from, subject) and Option 2 (body of the message). I'm also gambling on the fact that people actually know a lot about socket programming and communicating with a POP3 server using the RFC 1939 protocol but it's a chance I am willing to take. I printed out the RFC 1939 protocol and understand how the RFC commands work but it's just parsing it to display just a particular portion that is throwing the program off.

Thanks.