in reply to long text message problem

I don't see in your code where you applied binmode. I think you will need to binmode both, the handle you're reading from and the handle you're writing to:

binmode ARGV; # <> reads from ARGV implicitly my $HL7 = <>; ... my $sock = ...; binmode $sock;