<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">##########################################################
##  Working implementation of the OSCAR - ICQ/AIM protocol
##  This module is part of Laffer Project http://laffer.sf.net
## (C) 2004-2006 Slavej Karadjov &lt;slaff@users.sf.net&gt;
## Based on vICQ module which is based on Net::ICQ2000 module written by Robin Fisher
## $Id: Oscar.pm,v 1.15 2006/02/15 07:41:38 slaff Exp $
##########################################################


##########################################################
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# See LICENSE for details
##########################################################
package Net::Oscar;


use strict;
no strict 'refs';
use vars qw(
  $VERSION
  $REVISION
  $DEVEL
  %_New_Connection_Nefotiation_Codes
  %_TLV_OUT %_TLV_IN %_TLV_Length_O %_TLV_Length_I %_Srv_Codes
  %_Srv_Decoder %_Cmd_Codes %_Cmd_Encoder %_DC_Decoder %_FileDC_Decoder
  %_ICQ_Versions %_Error_Codes
);

# in releases devel=0
$DEVEL=1;

use Time::Local;
use IO::Socket;
use IO::Select;
use POSIX qw(:errno_h);
use Math::BigInt;

my $Id='$Id: Oscar.pm,v 1.15 2006/02/15 07:41:38 slaff Exp $';
($REVISION) = $Id=~/ (\d+\.\d+) /;
if ($DEVEL){
	$VERSION='devel';
}else{
	$VERSION='1.00';
}
my $second = 0;


my @client_check_data = split //,"As part of this software beta version Mirabilis is granting a limited access to the ICQ network, servers, directories, listings, information and databases (\"ICQ Services and Information\"). The ICQ Service and Information may databases (\"ICQ Services and Information\"). The ICQ Service and Information may\0";
foreach (@client_check_data)
{
	$_ = ord;
}

sub new
{
	my($Package, $UIN, $Password, $AutoConnect, $ServerAddress, $ServerPort, $isAIM) = @_;

	$ServerAddress = "login.icq.com" unless $ServerAddress;
	$ServerPort = "5190" unless $ServerPort;

	my $Me = {
		_UIN =&gt; $UIN,
		_Password =&gt; $Password,
		_Server =&gt; $ServerAddress,
		_ServerPort =&gt; $ServerPort,
		_Socket =&gt; undef,
		_Select =&gt; undef,
		_DCSocket =&gt; undef,
		_FileSocket =&gt; undef,
		_ID =&gt; 0,
		_DCs =&gt; [],
		_FileDCs =&gt; [],
		_DCSelects =&gt; [],
		_DCPort =&gt; 0,
		_FilePort =&gt; 0,
		_ChatPort =&gt; 0, # for future :)
		_DCIP =&gt; '0.0.0.0',
		_Seq_Num =&gt; int(rand(0xFFFF)),
		_Incoming_Queue =&gt; [],
		_Outgoing_Queue =&gt; [],
		_Connection_Cookie =&gt; 0,
		_Incoming_Dir =&gt; '',
		_Hooks =&gt; {},
		_Connected =&gt; 0,
		_LoggedIn =&gt; 0,
		_FLAP_Bytes_Left =&gt; 0,
		_FLAP_Header_Bytes_Left =&gt; 6,
		_FLAP_SubHeader_Bytes_Left =&gt; 4,
		_FLAP_Header =&gt; [],
		_FLAP_Sub_Header =&gt; [],
		_FLAP_In_progress =&gt; undef,
		_Mem =&gt; 1,
		_Auto_Login =&gt; 1, #one means minimum, two means full ICQ logon, 0 means none/developer deals with it..
		_Auto_Login_Contact_List =&gt; [],
		_Auto_Login_Visible_List =&gt; [],
		_Auto_Login_Invisible_List =&gt; [],
		_Sent_Requests =&gt; {},
		_Status =&gt; 0,
		_Proxy_Host =&gt; '',
		_Proxy_Port =&gt; '',
		_Proxy_Login =&gt; '',
		_Proxy_Password =&gt; '',
		_Force_HTTP_Port =&gt; '',
		_Proxy_Type =&gt; '',
		_Debug =&gt; 0,
		_Register =&gt; 0,
		_NewUIN =&gt; 0,
		_Hide_IP =&gt; 0,
		_ErrorStr=&gt; '',
		_ErrorCode=&gt; 0,
		_Away_Message =&gt; 'I am away!',
		_NA_Message =&gt; 'Not available for this moment',
		_Occupied_Message =&gt; 'Occupied!',
		_DND_Message =&gt; 'Do Not Disturb!!!!',
		_FFC_Message =&gt; 'Let\'s talk!',
		_Capabilities =&gt; [
					0x09461349,0x4c7f11d1,0x82224445,0x53540000,
					0x09461344,0x4c7f11d1,0x82224445,0x53540000,
				 ],
		_TimeStamp1 =&gt; 0x04031980,
		_TimeStamp2 =&gt; 0,
		_TimeStamp3 =&gt; time(),
		_isAIM	   =&gt; $isAIM,
	};
	bless($Me, $Package);

	if($Me-&gt;{_isAIM}) {
		push(@{$Me-&gt;{_Capabilities}},0x0946134D,0x4C7F11D1,0x82224445,0x53540000); # can send messages to ICQ and AIM users
	}

	$Me-&gt;Connect() if $AutoConnect;

	return $Me;
}

sub SetID
{
	my($Me, $id) = @_;
	$Me-&gt;{_TimeStamp2} = $id;
}

sub IdentifyClientAs {
	my ($Me, $client_name, $client_version) = @_;

	# [CAPABILITIES] #
	# For more information about capabilities take a look at
	# 	http://www.stud.uni-karlsruhe.de/~uck4/ICQ/Define/CAPABILITIES


	# [TIME STAMPS] #
	# For more information about timestamp identification take a look at
	#	http://www.stud.uni-karlsruhe.de/~uck4/ICQ/Packet-40.html

	return 0 if(!$client_name);

	$client_name = uc($client_name);

	print "Net::vICQ: client name: ".$client_name."\n";

	if($client_name eq "MIRANDA") {
		$Me-&gt;{_TimeStamp1} = 0xFFFFFFFF;
		my ($major_ver, $minor_ver, $rev_ver) = split(/\./,$client_version);
		$Me-&gt;{_TimeStamp2} = hex($major_ver.$minor_ver.$rev_ver."00");
	}
	elsif($client_name eq "GAIM") {
		$Me-&gt;{_TimeStamp1} = 0xFFFFFFFF;
		$Me-&gt;{_TimeStamp2} = 0xFFFFFFFF;
		$Me-&gt;{_TimeStamp3} = 0xFFFFFFFF;

	}
	elsif($client_name eq "TRILLIAN") {
		push(@{$Me-&gt;{_Capabilities}}, 0xF2E7C7F4,0xFEAD4DFB, 0xB2353679, 0x8BDF0000);
	}
	elsif($client_name eq "MICQ") {
		my ($major_ver, $minor_ver, $rev_ver) = split(/\./,$client_version);
		push(@{$Me-&gt;{_Capabilities}}, 0x6d494351,0x20a92052, 0x2e4b2e20, hex($major_ver.$minor_ver.$rev_ver."00"));
	}
	elsif($client_name eq "SIM") {
		my ($major_ver, $minor_ver, $rev_ver) = split(/\./,$client_version);
		push(@{$Me-&gt;{_Capabilities}}, 0x97B12751, 0x243C4334, 0xAD22D6AB, hex("F73F14".(($major_ver + 1) &lt;&lt; 6 + $minor_ver)));
	}
	return 1;
}

sub Connect
{
	my($Me) = @_;

	return if $Me-&gt;{_Connected};

	if(!$Me-&gt;{_UIN}) {
		$Me-&gt;SetError("Attempted to connect without UIN!");
		return;
	}

	if(!$Me-&gt;{_Password}) {
		$Me-&gt;SetError("Attempted to connect without Password!");
		return;
	}

	if($Me-&gt;{_Proxy_Type} eq 'socks') {
		my $proxy_host = $Me-&gt;{_Proxy_Host};
		if($proxy_host eq '') {
			$Me-&gt;SetError("No proxy host specified");
			return;
		}
		my $proxy_port = $Me-&gt;{_Proxy_Port};
		if($proxy_port eq '') {
			$Me-&gt;SetError("No proxy port specified");
			return;
		}
		my $proxy_login = $Me-&gt;{_Proxy_Login};
		my $proxy_password = $Me-&gt;{_Proxy_Password};
		my $an = ($proxy_login eq '') ? 0 : 1;
		my $socks;
		my $socks_version = 5;
		eval {
			require Net::SOCKS;
		};
		if(!$@) {
			$socks = new Net::SOCKS(socks_addr =&gt; $proxy_host,
					socks_port =&gt; $proxy_port,
					user_id =&gt; $proxy_login,
					user_password =&gt; $proxy_password,
					force_nonanonymous =&gt; $an,
					protocol_version =&gt; $socks_version);

			$Me-&gt;{_Socket} = $socks-&gt;connect(peer_addr =&gt; $Me-&gt;{_Server}, peer_port =&gt; $Me-&gt;{_ServerPort});

			if($socks-&gt;param('status_num') != 90) {
				$Me-&gt;SetError("SOCKS Connection error: ",Net::SOCKS::status_message($socks-&gt;param('status_num')), "\n");
				return;
			}
		} else {
			die "You should install Net::SOCKS package to enable socks proxy support";
		}
	} elsif( $Me-&gt;{_Proxy_Type} eq 'https') {

		my $proxy_host = $Me-&gt;{_Proxy_Host};
		if($proxy_host eq '') {
			$Me-&gt;SetError("No proxy host specified");
			return;
		}

		my $proxy_port = $Me-&gt;{_Proxy_Port};
		if($proxy_port eq '') {
			$Me-&gt;SetError("No proxy port specified");
			return;
		}

		my $proxy_login = $Me-&gt;{_Proxy_Login};
		my $proxy_password = $Me-&gt;{_Proxy_Password};
		if ( $Me-&gt;{_Force_HTTPS_Port} ) {
			$Me-&gt;{_ServerPort} = "443";
		}

		$Me-&gt;{_Socket} = IO::Socket::INET-&gt;new( Proto =&gt; "tcp",
							PeerAddr =&gt; $proxy_host,
							PeerPort =&gt; $proxy_port);
		if(!$Me-&gt;{_Socket}) {
			$Me-&gt;SetError("socket error[https proxy]: $@");
			return;
		}

		my $auth = "Proxy-Authorization: Basic " . base64encode("$proxy_login:$proxy_password");
		$Me-&gt;{_Socket}-&gt;send("CONNECT $Me-&gt;{_Server}:$Me-&gt;{_ServerPort} HTTP/1.0\n$auth\n\n");
	 } else {
			$Me-&gt;{_Socket} = IO::Socket::INET-&gt;new(
						Proto	 =&gt; "tcp",
						PeerAddr =&gt; $Me-&gt;{_Server},
						PeerPort =&gt; $Me-&gt;{_ServerPort});
			if(!$Me-&gt;{_Socket}) {
				$Me-&gt;SetError("socket error[server]: $@");
				return;
			}
	}

	$Me-&gt;{_Select} = IO::Select-&gt;new($Me-&gt;{_Socket});
	$Me-&gt;{_Connected} = 1;
}


sub GetError
{
	my($Me) = @_;
	my $s = $Me-&gt;{_ErrorStr};
	$Me-&gt;ResetError();
	return $s;
}

sub GetErrorCode
{
	my($Me) = @_;
	my $s = $Me-&gt;{_ErrorStr};
	my $c = $Me-&gt;{_ErrorCode};
	$Me-&gt;ResetError();
	return ($c, $s) if($c);
	return ();
}


sub ResetError
{
	my($Me) = @_;
	$Me-&gt;{_ErrorStr} = "";
	$Me-&gt;{_ErrorCode} = 0;
}

sub SetError
{
	my($Me,$code,@msgs) = @_;
	$Me-&gt;{_ErrorCode} = -1;
	$Me-&gt;{_ErrorStr} = join '',@msgs;
}

sub SetErrorCode
{
	my($Me,$code) = @_;
	$Me-&gt;{_ErrorCode} = $code;
	$Me-&gt;{_ErrorStr} = exists($_Error_Codes{$code}) ?  $_Error_Codes{$code} :
		"Error code $code";
}

sub InitDCServer
{
	my($Me,$LocalAddr,$LocalPort) = @_;
	$LocalAddr = '0.0.0.0' unless $LocalAddr;
	$LocalPort = undef unless $LocalPort;
	$Me-&gt;{_DCSocket} = IO::Socket::INET-&gt;new(Listen =&gt; 5,
					LocalAddr =&gt; $LocalAddr,
					LocalPort =&gt; $LocalPort,
					Proto	=&gt; 'tcp',
					Reuse	=&gt; 1,
					Timeout	=&gt; .005);

	if(! defined $Me-&gt;{_DCSocket}) {
		$Me-&gt;SetError("listen socket error: $!");
		# return $Me;
	} else {
		my $myaddrstruct = $Me-&gt;{_DCSocket}-&gt;sockname();
		my ($port, $myaddr) = sockaddr_in($myaddrstruct);
		$Me-&gt;{_DCIP} = inet_ntoa($myaddr);
		$Me-&gt;{_DCPort} = $port;
	}
}

sub InitFileServer
{
	my($Me,$LocalAddr,$LocalPort) = @_;
	$LocalAddr = '0.0.0.0' unless $LocalAddr;
	$LocalPort = undef unless $LocalPort;
	$Me-&gt;{_FileSocket} = IO::Socket::INET-&gt;new(Listen =&gt; 5,
					LocalAddr =&gt; $LocalAddr,
					LocalPort =&gt; $LocalPort,
					Proto	=&gt; 'tcp',
					Reuse	=&gt; 1,
					Timeout	=&gt; .005);

	if(! defined $Me-&gt;{_FileSocket}) {
		$Me-&gt;SetError("listen socket error: $!");
		# return $Me;
	} else {
		my $myaddrstruct = $Me-&gt;{_FileSocket}-&gt;sockname();
		my ($port, $myaddr) = sockaddr_in($myaddrstruct);
		$Me-&gt;{_FilePort} = $port;
	}
}


sub Disconnect
{
	my($Me) = @_;

	$Me-&gt;{_Connected} or return;

	close($Me-&gt;{_Socket});
	$Me-&gt;{_Select} = undef;
	$Me-&gt;{_Connected} = 0;
	$Me-&gt;{_LoggedIn} = 0;
	$Me-&gt;{_Incoming_Queue} = [];
	$Me-&gt;{_Outgoing_Queue} = [];
	$Me-&gt;{_Server} = 'login.icq.com';
	$Me-&gt;{_ServerPort} = 5190;
}


sub Send_Keep_Alive
{
	my($Me, $UIN, $Pass) = @_;
	return if (!$Me-&gt;{_Connected});
	my($Responce);
	####
	# I didnt find any proper info about keep-alive packets
	# except that its use fifth channel
	# at least it doesnt cause errors - let it be
	####
	$Responce-&gt;{Channel_ID} = 5;
	push(@{$Me-&gt;{_Outgoing_Queue}}, $Responce);

}

sub Register
{
	my $Me = shift;
	my $uin = 0;
	my $i =0;
	$Me-&gt;{_Register} = 1;
	$Me-&gt;{_NewUIN} = 0;
	while(($Me-&gt;{'_ErrorStr'} eq '') &amp;&amp; (!$Me-&gt;{_NewUIN}) &amp;&amp; $Me-&gt;{_Connected})
	{
		$Me-&gt;Check_Incoming if $Me-&gt;{_Connected};
		$Me-&gt;Deal_With_FLAPs if $Me-&gt;{_Connected};
		$Me-&gt;Send_Outgoing if $Me-&gt;{_Connected};
		select(undef, undef, undef, 0.25);
	}
	$Me-&gt;{_Register} = 0;
	return $Me-&gt;{_NewUIN};
}

sub Set_Login_Details
{
	my($Me, $UIN, $Pass) = @_;

	return if $Me-&gt;{_Connected};

	$Me-&gt;{_UIN} = $UIN if $UIN;
	$Me-&gt;{_Password} = $Pass if $Pass;
}


sub Execute_Once
{
	my ($Me) = @_;

	$Me-&gt;Check_Incoming if $Me-&gt;{_Connected};
	$Me-&gt;Deal_With_FLAPs if $Me-&gt;{_Connected};
	$Me-&gt;Send_Outgoing if $Me-&gt;{_Connected};
	$Me-&gt;HandleDC if $Me-&gt;{_DCSocket};
	$Me-&gt;HandleFileDC if $Me-&gt;{_FileSocket};
}

sub Send_Command
{
	my ($Me, $Command, $Details) = @_;
	(exists $_Cmd_Codes{$Command}) or return;

	&amp;{$_Cmd_Encoder{$_Cmd_Codes{$Command}}}($Me, $Details) if (exists $_Cmd_Encoder{$_Cmd_Codes{$Command}});
}

sub Add_Hook
{
	my($Me, $HookType, $HookFunction) = @_;

	$_Srv_Codes{$HookType} or die("Bad Hook type!\n");
	$Me-&gt;{_Hooks}{$_Srv_Codes{$HookType}} = $HookFunction;
}


%_ICQ_Versions = (
	4 =&gt; 'icq98',
	6 =&gt; 'licq',
	7 =&gt; 'icq2000',
	8 =&gt; 'icq2001');

%_Cmd_Encoder = (
	#Cmd_GSC_Client_Ready
	'1:2' =&gt; sub {
		my($Me, $event) = @_;
		my($Responce);

		@{$Responce-&gt;{Data_Load}} = &amp;_Make_SNAC_Header(1, 2, 0, 0, 2);

		push(@{$Responce-&gt;{Data_Load}}, _int_to_bytes(2, 1));
		push(@{$Responce-&gt;{Data_Load}}, _int_to_bytes(2, 3));
		push(@{$Responce-&gt;{Data_Load}}, _int_to_bytes(2, 0x0110));
		push(@{$Responce-&gt;{Data_Load}}, _int_to_bytes(2, 0x028a));
		push(@{$Responce-&gt;{Data_Load}}, _int_to_bytes(2, 2));
		push(@{$Responce-&gt;{Data_Load}}, _int_to_bytes(2, 1));
		push(@{$Responce-&gt;{Data_Load}}, _int_to_bytes(2, 0x0101));
		push(@{$Responce-&gt;{Data_Load}}, _int_to_bytes(2, 0x028a));
		push(@{$Responce-&gt;{Data_Load}}, _int_to_bytes(2, 3));
		push(@{$Responce-&gt;{Data_Load}}, _int_to_bytes(2, 1));
		push(@{$Responce-&gt;{Data_Load}}, _int_to_bytes(2, 0x0110));
		push(@{$Responce-&gt;{Data_Load}}, _int_to_bytes(2, 0x028a));
		push(@{$Responce-&gt;{Data_Load}}, _int_to_bytes(2, 0x15));
		push(@{$Responce-&gt;{Data_Load}}, _int_to_bytes(2, 1));
		push(@{$Responce-&gt;{Data_Load}}, _int_to_bytes(2, 0x0110));
		push(@{$Responce-&gt;{Data_Load}}, _int_to_bytes(2, 0x028a));
		push(@{$Responce-&gt;{Data_Load}}, _int_to_bytes(2, 4));
		push(@{$Responce-&gt;{Data_Load}}, _int_to_bytes(2, 1));
		push(@{$Responce-&gt;{Data_Load}}, _int_to_bytes(2, 0x0110));
		push(@{$Responce-&gt;{Data_Load}}, _int_to_bytes(2, 0x028a));
		push(@{$Responce-&gt;{Data_Load}}, _int_to_bytes(2, 6));
		push(@{$Responce-&gt;{Data_Load}}, _int_to_bytes(2, 1));
		push(@{$Responce-&gt;{Data_Load}}, _int_to_bytes(2, 0x0110));
		push(@{$Responce-&gt;{Data_Load}}, _int_to_bytes(2, 0x028a));
		push(@{$Responce-&gt;{Data_Load}}, _int_to_bytes(2, 9));
		push(@{$Responce-&gt;{Data_Load}}, _int_to_bytes(2, 1));
		push(@{$Responce-&gt;{Data_Load}}, _int_to_bytes(2, 0x0110));
		push(@{$Responce-&gt;{Data_Load}}, _int_to_bytes(2, 0x028a));
		push(@{$Responce-&gt;{Data_Load}}, _int_to_bytes(2, 0x0a));
		push(@{$Responce-&gt;{Data_Load}}, _int_to_bytes(2, 1));
		push(@{$Responce-&gt;{Data_Load}}, _int_to_bytes(2, 0x0110));
		push(@{$Responce-&gt;{Data_Load}}, _int_to_bytes(2, 0x028a));

		push(@{$Me-&gt;{_Outgoing_Queue}}, $Responce);

		#turn off the auto login, to save processor time..
		$Me-&gt;{_Auto_Login} = 0;
	},
	#Cmd_GSC_Reqest_Rate_Info
	'1:6' =&gt; sub {
		my($Me, $event) = @_;
		my($Responce);

		@{$Responce-&gt;{Data_Load}} = &amp;_Make_SNAC_Header(1, 6, 0, 0, 6);
		push(@{$Me-&gt;{_Outgoing_Queue}}, $Responce);
	},
	#Cmd_GSC_Rate_Info_Ack
	'1:8' =&gt; sub {
		my($Me, $event) = @_;
		my($Responce);

		@{$Responce-&gt;{Data_Load}} = &amp;_Make_SNAC_Header(1, 8, 0, 0, 8);

		# another junk filled response (AOL must like using up
		# network resources..)

		push(@{$Responce-&gt;{Data_Load}}, (0,1,0,2,0,3,0,4,0,5));
		push(@{$Me-&gt;{_Outgoing_Queue}}, $Responce);
	},
	#Cmd_GSC_LoggedIn_User_Info
	'1:14' =&gt; sub {
		my($Me, $event) = @_;
		my($Responce);

		@{$Responce-&gt;{Data_Load}} = &amp;_Make_SNAC_Header(1, 14, 0, 0, 14);

		push(@{$Me-&gt;{_Outgoing_Queue}}, $Responce);
	},
	#Cmd_GSC_ICQInform
	'1:23' =&gt; sub {
		my($Me, $event) = @_;
		my($Responce);

		#Never changes..
		@{$Responce-&gt;{Data_Load}} = &amp;_Make_SNAC_Header(1, 0x17, 0, 0, 0x17);
		push(@{$Responce-&gt;{Data_Load}}, (0,1,0,3,0,2,0,1,0,3,0,1,0,21,0,1,0,4,0,1,0,6,0,1,0,9,0,1,0,10,0,1));
# 		push(@{$Responce-&gt;{Data_Load}}, (0,1,0,3,0,2,0,1,0,3,0,1,0,21,0,1,0,4,0,1,0,6,0,1,0,8,0,1,0,9,0,1,0,10,0,1,0,11,0,1,0,12,0,1,0,0x13,4,0x15,1,0x22,1));
		push(@{$Me-&gt;{_Outgoing_Queue}}, $Responce);
	},
	#Cmd_GSC_Set_Status
	'1:30' =&gt; sub {
		my($Me, $event) = @_;
		my($Responce, $Responce2);

		@{$Responce-&gt;{Data_Load}} = &amp;_Make_SNAC_Header(1, 30, 0, 0, 30);

		my $status = $event-&gt;{Status} | ($Me-&gt;{_Hide_IP} ? 0x10000000 : 0);
		push(@{$Responce-&gt;{Data_Load}}, _Write_TLV(2, 'Status', $status));
		push(@{$Responce-&gt;{Data_Load}}, _Write_TLV(2, 'ErrorCode', 0));
		push(@{$Responce-&gt;{Data_Load}}, _int_to_bytes(4,0x000c0025));
		# if(!$Me-&gt;{_LoggedIn})
		{
			my $ip = inet_aton($Me-&gt;{_DCIP});
			my $port = $Me-&gt;{_DCPort};
			my $l = length($ip);
			if(!$Me-&gt;{_Hide_IP})
			{
				# IP
				push(@{$Responce-&gt;{Data_Load}}, _int_to_bytes(4,$ip));
				# Port
				push(@{$Responce-&gt;{Data_Load}}, _int_to_bytes(4,$port));
				# Type of conn
				push(@{$Responce-&gt;{Data_Load}}, _int_to_bytes(1,4));
				# Version
				push(@{$Responce-&gt;{Data_Load}}, _int_to_bytes(2,0x0007));
			} else
			{
				push(@{$Responce-&gt;{Data_Load}}, _int_to_bytes(4,0));
				push(@{$Responce-&gt;{Data_Load}}, _int_to_bytes(4,0));
				push(@{$Responce-&gt;{Data_Load}}, _int_to_bytes(1,0));
				push(@{$Responce-&gt;{Data_Load}}, _int_to_bytes(2,0));
			}

			push(@{$Responce-&gt;{Data_Load}}, _int_to_bytes(4,time())); # ???
			push(@{$Responce-&gt;{Data_Load}}, _int_to_bytes(4,0x00000050)); # ???
			push(@{$Responce-&gt;{Data_Load}}, _int_to_bytes(4,0x00000003)); # ???
			push(@{$Responce-&gt;{Data_Load}}, _int_to_bytes(4,$Me-&gt;{_TimeStamp1}));
			push(@{$Responce-&gt;{Data_Load}}, _int_to_bytes(4,$Me-&gt;{_TimeStamp2}));
			push(@{$Responce-&gt;{Data_Load}}, _int_to_bytes(4,$Me-&gt;{_TimeStamp3}));
			push(@{$Responce-&gt;{Data_Load}}, _int_to_bytes(2,0000));
			push(@{$Responce-&gt;{Data_Load}}, _int_to_bytes(4,0x012C35FB));
			push(@{$Responce-&gt;{Data_Load}}, _int_to_bytes(1,0x3b));
		}
		push(@{$Responce-&gt;{Data_Load}}, _int_to_bytes(4,0x00120002)); # TLV
		push(@{$Responce-&gt;{Data_Load}}, _int_to_bytes(2,0)); # ???
		push(@{$Me-&gt;{_Outgoing_Queue}}, $Responce);

		# send the "Made Change/update command" (really I don't know
		# whta this is for..)
		@{$Responce2-&gt;{Data_Load}} = &amp;_Make_SNAC_Header(1, 17, 0, 0, 17);
		push(@{$Responce2-&gt;{Data_Load}}, _int_to_bytes(4, 0));
		push(@{$Me-&gt;{_Outgoing_Queue}}, $Responce2);
	},

	#Cmd_LS_LoggedIn_User_Rights
	'2:2' =&gt; sub {
		my($Me, $event) = @_;
		my($Responce);

		@{$Responce-&gt;{Data_Load}} = &amp;_Make_SNAC_Header(2, 2, 0, 0, 2);

		push(@{$Me-&gt;{_Outgoing_Queue}}, $Responce);
	},
	#Cmd_LS_Set_User_Info
	'2:4' =&gt; sub {
		my($Me, $event) = @_;
		my($Responce);

		@{$Responce-&gt;{Data_Load}} = &amp;_Make_SNAC_Header(2, 4, 0, 0, 4);

		# if this is setting our details, shouldn't we set something?
		# maybe later.. : )

		push(@{$Responce-&gt;{Data_Load}}, _int_to_bytes(2, 5));
		push(@{$Responce-&gt;{Data_Load}}, _int_to_bytes(2, 32));

		foreach (@{$Me-&gt;{_Capabilities}})
		{
			push(@{$Responce-&gt;{Data_Load}}, _int_to_bytes(4,$_));
		}

		push(@{$Me-&gt;{_Outgoing_Queue}}, $Responce);
	},
	#Cmd_BLM_Rights_Info
	'3:2' =&gt; sub {
		my($Me, $event) = @_;
		my($Responce);

		@{$Responce-&gt;{Data_Load}} = &amp;_Make_SNAC_Header(3, 2, 0, 0, 2);

		push(@{$Me-&gt;{_Outgoing_Queue}}, $Responce);
	},
	#Cmd_CTL_UploadList
	'3:4' =&gt; sub {
		my($Me, $event) = @_;
		my($Responce);
		my $nRequestID = $Me-&gt;_getRequestID(4);
		@{$Responce-&gt;{Data_Load}} = &amp;_Make_SNAC_Header(3, 4, 0, 0, $nRequestID);

		#don't send the command unless we have a list to send..
		return if ($#{$event-&gt;{ContactList}} == -1);

		foreach (@{$event-&gt;{ContactList}}){
			push(@{$Responce-&gt;{Data_Load}}, _int_to_bytes(1, length($_)));
			push(@{$Responce-&gt;{Data_Load}}, _str_to_bytes($_));
		}
		push(@{$Me-&gt;{_Outgoing_Queue}}, $Responce);
		return $nRequestID;
	},
	#Cmd_Mes_Add_ICBM_Param
	'4:2' =&gt; sub {
		my($Me, $event) = @_;
		my($Responce);
		@{$Responce-&gt;{Data_Load}} = &amp;_Make_SNAC_Header(4, 2, 0, 0, 2);

		# http://iserverd1.khstu.ru/oscar/snac_04_02.html
		# binary = 1011 means bit1,2,4 are set 4 is responsible for client notifications


# 		push(@{$Responce-&gt;{Data_Load}}, (0,0,0,0,0,3,0x1f,0x40,3,0xe7,3,0xef,0,0,0,0));
		push(@{$Responce-&gt;{Data_Load}}, (0,0,0,0,0,11,0x1f,0x40,3,0xe7,3,0xef,0,0,0,0));
		push(@{$Me-&gt;{_Outgoing_Queue}}, $Responce);
	},
	#Cmd_Mes_Param_Info
	'4:4' =&gt; sub {
		my($Me, $event) = @_;
		my($Responce);

		@{$Responce-&gt;{Data_Load}} = &amp;_Make_SNAC_Header(4, 4, 0, 0, 4);

		push(@{$Me-&gt;{_Outgoing_Queue}}, $Responce);
	},
	#Cmd_Send_Message
	'4:6' =&gt; sub {
		my($Me, $event) = @_;
		my($Responce, @TempPacket);
		if($event-&gt;{MessageType} eq 'text') {
			@{$Responce-&gt;{Data_Load}} = &amp;_Make_SNAC_Header(4, 6, 0, 1, 6);
			my $uin = $event-&gt;{uin};
			my $msg = $event-&gt;{text};
			my $len = length($msg);
			push(@TempPacket, _int_to_bytes(4,0x52995d00));
			push(@TempPacket, _int_to_bytes(4,0x69230000));
			push(@TempPacket, _int_to_bytes(2,0x0001));
			push(@TempPacket, _uin_to_buin($uin));

			push(@TempPacket, _int_to_bytes(2,0x0002)); # TLV: Message Block
			push(@TempPacket, _int_to_bytes(2,$len+14));
			push(@TempPacket, _int_to_bytes(2,0x0501)); # Features 1
			push(@TempPacket, _int_to_bytes(2,0x0002)); # Features Length 1
			push(@TempPacket, _int_to_bytes(2,0x0106)); # Features 2
			push(@TempPacket, _int_to_bytes(2,0x0101)); # Block Info 2
			push(@TempPacket, _int_to_bytes(2,$len+4)); # Features Length 2
			push(@TempPacket, _int_to_bytes(2,$event-&gt;{charset})); # Charset 2
			push(@TempPacket, _int_to_bytes(2,$event-&gt;{subcharset})); # Sub-Charset 2
			push(@TempPacket, _str_to_bytes($msg));

			push(@TempPacket, _int_to_bytes(2,0x0003)); # TLV: Server Ack Requested
			push(@TempPacket, _int_to_bytes(2,0));

			push(@TempPacket, _int_to_bytes(2,0x0006)); # TLV: Store message if user is offline
			push(@TempPacket, _int_to_bytes(2,0));

			push(@{$Responce-&gt;{Data_Load}}, @TempPacket);
			push(@{$Me-&gt;{_Outgoing_Queue}}, $Responce);
		} elsif($event-&gt;{MessageType} eq 'url') {
			@{$Responce-&gt;{Data_Load}} = &amp;_Make_SNAC_Header(4, 6, 0,0,0x00010006);
			my $uin = $event-&gt;{uin};
			my $url = $event-&gt;{URL};
			my $desc = $event-&gt;{Description};
			my $msg = $desc . "\xfe" . $url;
			my $len = length($msg)+9;
			push(@TempPacket, _int_to_bytes(4,0x52995d00));
			push(@TempPacket, _int_to_bytes(4,0x69230000));
			push(@TempPacket, _int_to_bytes(2,0x0004));
			push(@TempPacket, _uin_to_buin($uin));
			push(@TempPacket, _int_to_bytes(2,0x0005)); #TLV
			push(@TempPacket, _int_to_bytes(2,$len)); # TLV
			push(@TempPacket, _int_to_endian_bytes(4,$Me-&gt;{_UIN}));
			push(@TempPacket, _int_to_bytes(2,0x0400)); # flags

			$msg = "$msg\x00";
			$len = length($msg);
			push(@TempPacket, _int_to_endian_bytes(2,$len)); # TLV
			push(@TempPacket, _str_to_bytes($msg));
			push(@TempPacket, _int_to_bytes(2,0x0006)); # final TLV
			push(@TempPacket, _int_to_bytes(2,0x0000)); # Final TLV
			push(@{$Responce-&gt;{Data_Load}}, @TempPacket);
			push(@{$Me-&gt;{_Outgoing_Queue}}, $Responce);

		}
	},
	# ACK to type-2 messages
	'4:11' =&gt; sub {
		my($Me, $event) = @_;
		my($Responce);
		@{$Responce-&gt;{Data_Load}} = &amp;_Make_SNAC_Header(4, 11, 0, 0, 11);
		push(@{$Responce-&gt;{Data_Load}}, _str_to_bytes($event-&gt;{first_bytes}));
		push(@{$Responce-&gt;{Data_Load}}, _uin_to_buin($event-&gt;{UIN}));
		push(@{$Responce-&gt;{Data_Load}}, _int_to_bytes(2, 0x0003));
		push(@{$Responce-&gt;{Data_Load}}, _str_to_bytes($event-&gt;{chunk1}));
		push(@{$Responce-&gt;{Data_Load}}, _str_to_bytes($event-&gt;{chunk2}));
		push(@{$Responce-&gt;{Data_Load}}, _int_to_bytes(1, $event-&gt;{type}));
		push(@{$Responce-&gt;{Data_Load}}, _int_to_bytes(1, $event-&gt;{flags}));
		push(@{$Responce-&gt;{Data_Load}}, _int_to_bytes(1, $event-&gt;{accept_status}));
		push(@{$Responce-&gt;{Data_Load}}, _int_to_bytes(3, 0x0));
		push(@{$Responce-&gt;{Data_Load}}, _int_to_endian_bytes(2,length($event-&gt;{msg})+1));
		push(@{$Responce-&gt;{Data_Load}}, _str_to_bytes($event-&gt;{msg} . "\x0"));
		if($event-&gt;{type} == 1)
		{
			push(@{$Responce-&gt;{Data_Load}}, _int_to_bytes(4, 0x00000000));
			push(@{$Responce-&gt;{Data_Load}}, _int_to_bytes(4, 0xffffffff));
		}
		push(@{$Me-&gt;{_Outgoing_Queue}}, $Responce);
	},

	#Cmd_BOS_Get_Rights
	'9:2' =&gt; sub {
		my($Me, $event) = @_;
		my($Responce);

		@{$Responce-&gt;{Data_Load}} = &amp;_Make_SNAC_Header(9, 2, 0, 0, 2);

		push(@{$Me-&gt;{_Outgoing_Queue}}, $Responce);
	},
	#Cmd_BOS_Add_VisibleList
	'9:5' =&gt; sub {
		my($Me, $event) = @_;
		my($Responce);

		@{$Responce-&gt;{Data_Load}} = &amp;_Make_SNAC_Header(9, 5, 0, 0, 5);

		foreach (@{$event-&gt;{VisibleList}}){
			push(@{$Responce-&gt;{Data_Load}}, _int_to_bytes(1, length($_)));
			push(@{$Responce-&gt;{Data_Load}}, _str_to_bytes($_));
		}
		push(@{$Me-&gt;{_Outgoing_Queue}}, $Responce);
	},
	#Cmd_BOS_Remove_VisibleList
	'9:6' =&gt; sub {
		my($Me, $event) = @_;
		my($Responce);

		@{$Responce-&gt;{Data_Load}} = &amp;_Make_SNAC_Header(9, 6, 0, 0, 6);

		foreach (@{$event-&gt;{VisibleList}}){
			push(@{$Responce-&gt;{Data_Load}}, _int_to_bytes(1, length($_)));
			push(@{$Responce-&gt;{Data_Load}}, _str_to_bytes($_));
		}
		push(@{$Me-&gt;{_Outgoing_Queue}}, $Responce);
	},
	#Cmd_BOS_Add_InVisibleList
	'9:7' =&gt; sub {
		my($Me, $event) = @_;
		my($Responce);

		@{$Responce-&gt;{Data_Load}} = &amp;_Make_SNAC_Header(9, 7, 0, 0, 7);

		foreach (@{$event-&gt;{InVisibleList}}) {
			push(@{$Responce-&gt;{Data_Load}}, _int_to_bytes(1, length($_)));
			push(@{$Responce-&gt;{Data_Load}}, _str_to_bytes($_));
		}
		push(@{$Me-&gt;{_Outgoing_Queue}}, $Responce);
	},
	#Cmd_BOS_Remove_InVisibleList
	'9:8' =&gt; sub {
		my($Me, $event) = @_;
		my($Responce);

		@{$Responce-&gt;{Data_Load}} = &amp;_Make_SNAC_Header(9, 8, 0, 0, 8);

		foreach (@{$event-&gt;{InVisibleList}}) {
			push(@{$Responce-&gt;{Data_Load}}, _int_to_bytes(1, length($_)));
			push(@{$Responce-&gt;{Data_Load}}, _str_to_bytes($_));
		}
		push(@{$Me-&gt;{_Outgoing_Queue}}, $Responce);
	},
	# Srv_Password_Missmatch or Sev_BOS_Error
	'9:9' =&gt; sub {

	},
	#Cmd_BOS_Add_GenericList
	'9:10' =&gt; sub {
		my($Me, $event) = @_;
		my($Responce);

		@{$Responce-&gt;{Data_Load}} = &amp;_Make_SNAC_Header(9, 10, 0, 0, 10);

		foreach (@{$event-&gt;{GenericList}}) {
			push(@{$Responce-&gt;{Data_Load}}, _int_to_bytes(1, length($_)));
			push(@{$Responce-&gt;{Data_Load}}, _str_to_bytes($_));
		}
		push(@{$Me-&gt;{_Outgoing_Queue}}, $Responce);
	},
	#Cmd_BOS_Remove_GenericList
	'9:11' =&gt; sub {
		my($Me, $event) = @_;
		my($Responce);

		@{$Responce-&gt;{Data_Load}} = &amp;_Make_SNAC_Header(9, 11, 0, 0, 11);

		foreach (@{$event-&gt;{GenericList}}) {
			push(@{$Responce-&gt;{Data_Load}}, _int_to_bytes(1, length($_)));
			push(@{$Responce-&gt;{Data_Load}}, _str_to_bytes($_));
		}
		push(@{$Me-&gt;{_Outgoing_Queue}}, $Responce);
	},

	#Cmd_Init_Contacts
	'19:2' =&gt; sub {
		my($Me, $event) = @_;
		my($Responce, @TempPacket);
		@{$Responce-&gt;{Data_Load}} = &amp;_Make_SNAC_Header(19, 2, 0, 0, 2);
		push(@{$Responce-&gt;{Data_Load}}, @TempPacket);
		push(@{$Me-&gt;{_Outgoing_Queue}}, $Responce);
	},
	#Cmd_Get_List
	'19:5' =&gt; sub {
		my($Me, $event) = @_;
		my($Responce, @TempPacket);
		@{$Responce-&gt;{Data_Load}} = &amp;_Make_SNAC_Header(19, 5, 0, 0,0x010005);
		push(@TempPacket, _int_to_bytes(4, time()));
		push(@TempPacket, _int_to_bytes(2, 100));
		push(@{$Responce-&gt;{Data_Load}}, @TempPacket);
		push(@{$Me-&gt;{_Outgoing_Queue}}, $Responce);
	},
	#Cmd_Add_List
	'19:8' =&gt; sub {
		my($Me, $event) = @_;
		my($Responce, @TempPacket);
		my $nRequestID = $Me-&gt;_getRequestID(8);
		@{$Responce-&gt;{Data_Load}} = &amp;_Make_SNAC_Header(19, 8, 0, 0, $nRequestID);

		push(@TempPacket, _int_to_bytes(2, length($event-&gt;{'UIN'})));
		push(@TempPacket, _str_to_bytes($event-&gt;{'UIN'}));
		push(@TempPacket, _int_to_bytes(2, $event-&gt;{'GROUP_ID'}));
		push(@TempPacket, _int_to_bytes(2, $event-&gt;{'ITEM'})); # item id
		push(@TempPacket, _int_to_bytes(2, $event-&gt;{'TYPE'}));

		if($event-&gt;{'TYPE'}==0) {
				# length of TLVs length block
				my $len = 4;
				$len+=4 if($event-&gt;{'WAITING'});
				push(@TempPacket, _int_to_bytes(2,length($event-&gt;{'NAME'})+$len)); #TLV
				# what is the name of the user to add
				push(@TempPacket, _int_to_bytes(2,0x0131)); #TLV
				push(@TempPacket, _int_to_bytes(2, length($event-&gt;{'NAME'})));
				push(@TempPacket, _str_to_bytes($event-&gt;{'NAME'}));

				if($event-&gt;{'WAITING'}) {
					# add it as waiting authorization buddy
					push(@TempPacket, _int_to_bytes(2,0x0066)); #TLV
					push(@TempPacket, _int_to_bytes(2,0));
				}
		}
# 		elsif($event-&gt;{'TYPE'}==1) {
# 				# gaim adds this TLV
# 				push(@TempPacket, _int_to_bytes(2,0x0006)); #TLV
# 				push(@TempPacket, _int_to_bytes(2,0x00c8));
#				# Place Item Positions Here/ Internal Buddy IDs if you want to add users to the group
# 				push(@TempPacket, _int_to_bytes(2,0x0002));
# 				push(@TempPacket, _int_to_bytes(2,0x0001));
# 		}
		else {
				push(@TempPacket, _int_to_bytes(2, 0x0000));
		}

# FNAC
#         Family: SSI (0x0013)
#         Subtype: Add Buddy (0x0008)
#         FNAC Flags: 0x0000
#         FNAC ID: 0x00000017
# AIM Server Side Information Service
#     SSI Buddy Name length: 0x0006
#     Buddy Name: Spazik
#     SSI Buddy Group ID: 0x0002
#     SSI Buddy ID: 0x0000
#     SSI Buddy type: Group (0x0001)
#     SSI TLV Len: 0x0006
#     TLV: Unknown
#         Value ID: Unknown (0x00c8)
#         Length: 2
#         Value

		push(@{$Responce-&gt;{Data_Load}}, @TempPacket);
		push(@{$Me-&gt;{_Outgoing_Queue}}, $Responce);
		return $nRequestID;
	},
	# Cmd_Update_List
	'19:9' =&gt; sub {
		my($Me, $event) = @_;
		my($Responce, @TempPacket);
		my $nRequestID = $Me-&gt;_getRequestID(9);
		@{$Responce-&gt;{Data_Load}} = &amp;_Make_SNAC_Header(19, 9, 0, 0,$nRequestID);

		push(@TempPacket, _int_to_bytes(2, length($event-&gt;{'NAME'})));
		push(@TempPacket, _str_to_bytes($event-&gt;{'NAME'}));
		push(@TempPacket, _int_to_bytes(2, $event-&gt;{'GROUP_ID'}));
		push(@TempPacket, _int_to_bytes(2, $event-&gt;{'UIN'}));
		push(@TempPacket, _int_to_bytes(2, $event-&gt;{'TYPE'}));

		if(defined($event-&gt;{'ATTR'})) {
			if($event-&gt;{'TYPE'}==0) {
				my $len = 4;
				$len+=4 if($event-&gt;{'WAITING'});
				push(@TempPacket, _int_to_bytes(2,length($event-&gt;{'ATTR'})+$len)); #TLV
				push(@TempPacket, _int_to_bytes(2,0x0131)); #TLV
				push(@TempPacket, _int_to_bytes(2, length($event-&gt;{'ATTR'})));
				push(@TempPacket, _str_to_bytes($event-&gt;{'ATTR'}));

				if($event-&gt;{'WAITING'}) {
					# add it as waiting authorization buddy
					push(@TempPacket, _int_to_bytes(2,0x0066)); #TLV
					push(@TempPacket, _int_to_bytes(2,0));
				}
			}
			elsif($event-&gt;{'TYPE'}==1) {
				push(@TempPacket, _int_to_bytes(2,length($event-&gt;{'ATTR'})+6)); #TLV
				push(@TempPacket, _int_to_bytes(2,0x00c8)); #TLV
				push(@TempPacket, _int_to_bytes(2,0x0002)); # Lenght = 2
				push(@TempPacket, _int_to_bytes(2,$event-&gt;{'ATTR'}));
			}
			else {
				# we don't know how to handle TLV for different types
				push(@TempPacket, _int_to_bytes(2, 0x0000));
			}
		}
		else {
			push(@TempPacket, _int_to_bytes(2, 0x0000));
		}

		push(@{$Responce-&gt;{Data_Load}}, @TempPacket);
		push(@{$Me-&gt;{_Outgoing_Queue}}, $Responce);
		return $nRequestID;
	},
	# Cmd_Del_List
	'19:10' =&gt; sub {
		my($Me, $event) = @_;
		my($Responce, @TempPacket);
		my $nRequestID = $Me-&gt;_getRequestID(10);
		@{$Responce-&gt;{Data_Load}} = &amp;_Make_SNAC_Header(19, 10, 0, 0, $nRequestID);

		push(@TempPacket, _int_to_bytes(2, length($event-&gt;{'NAME'})));
		push(@TempPacket, _str_to_bytes($event-&gt;{'NAME'}));
		push(@TempPacket, _int_to_bytes(2, $event-&gt;{'GROUP_ID'}));
		push(@TempPacket, _int_to_bytes(2, $event-&gt;{'UIN'}));
		push(@TempPacket, _int_to_bytes(2, $event-&gt;{'TYPE'}));
		if($event-&gt;{'TYPE'}==0) {
			if(exists($event-&gt;{'ATTR'})) {
				push(@TempPacket, _int_to_bytes(2,length($event-&gt;{'ATTR'})+4)); #TLV
				push(@TempPacket, _int_to_bytes(2,0x0131)); #TLV
				push(@TempPacket, _int_to_bytes(2, length($event-&gt;{'ATTR'})));
				push(@TempPacket, _str_to_bytes($event-&gt;{'ATTR'}));
			}
			else {
				push(@TempPacket, _int_to_bytes(2, 0x0000));
			}
		}
		elsif($event-&gt;{'TYPE'}==1) {
			push(@TempPacket, _int_to_bytes(2, 0x0000));
		}

		push(@{$Responce-&gt;{Data_Load}}, @TempPacket);
		push(@{$Me-&gt;{_Outgoing_Queue}}, $Responce);
		return $nRequestID;
	},

	# Ext_RemoveVisibleList
# 	'19:10' =&gt; sub {
# 		my($Me, $event) = @_;
# 		my($Responce, @TempPacket);
# 		@{$Responce-&gt;{Data_Load}} = &amp;_Make_SNAC_Header(19, 8, 0, 0,0x020008);
# 		push(@{$Responce-&gt;{Data_Load}}, _int_to_bytes(1, 0x00));
# 		my $uin = $event-&gt;{UIN};
# 		push(@TempPacket, _uin_to_buin($uin)); #encode the ICQ num..
# 		push(@TempPacket, _int_to_bytes(4, 0x00005986));
# 		push(@TempPacket, _int_to_bytes(4, 0x00020000));
# 		push(@{$Responce-&gt;{Data_Load}}, @TempPacket);
# 		push(@{$Me-&gt;{_Outgoing_Queue}}, $Responce);
# 	},
	# Cmd_Start_ListUpdate
	'19:17'=&gt; sub {
		my($Me, $event) = @_;
		my($Responce, @TempPacket);
		@{$Responce-&gt;{Data_Load}} = &amp;_Make_SNAC_Header(19, 17, 0, 0, 17);
		if($event-&gt;{IMPORT_MODE}) {
			push(@TempPacket, _int_to_bytes(4,0x00010000));
		}
		push(@{$Responce-&gt;{Data_Load}}, @TempPacket);
		push(@{$Me-&gt;{_Outgoing_Queue}}, $Responce);
	},
	# Cmd_Stop_ListUpdate
	'19:18' =&gt; sub {
		my($Me, $event) = @_;
		my($Responce, @TempPacket);
		@{$Responce-&gt;{Data_Load}} = &amp;_Make_SNAC_Header(19, 18, 0, 0, 18);

		push(@{$Responce-&gt;{Data_Load}}, @TempPacket);
		push(@{$Me-&gt;{_Outgoing_Queue}}, $Responce);
	},
	# Cmd_Req_Auth
	'19:24' =&gt; sub {
		my($Me, $event) = @_;
		my($Responce, @TempPacket);
		my $nRequestID = $Me-&gt;_getRequestID(24);
		@{$Responce-&gt;{Data_Load}} = &amp;_Make_SNAC_Header(19, 24, 0, 0, $nRequestID);
		push(@TempPacket, _uin_to_buin($event-&gt;{UIN}));
		push(@TempPacket, _int_to_bytes(2,length($event-&gt;{REASON})));
		push(@TempPacket, _str_to_bytes($event-&gt;{REASON}));
		push(@TempPacket, _int_to_bytes(2,0));

		push(@{$Responce-&gt;{Data_Load}}, @TempPacket);
		push(@{$Me-&gt;{_Outgoing_Queue}}, $Responce);
		return $nRequestID;
	},
	#Cmd_Authorize
	'19:26' =&gt; sub {
		my($Me, $event) = @_;
		my($Responce, @TempPacket);
		my $nRequestID = $Me-&gt;_getRequestID(26);
		@{$Responce-&gt;{Data_Load}} = &amp;_Make_SNAC_Header(19, 26, 0, 0, $nRequestID);
		push(@TempPacket, _uin_to_buin($event-&gt;{uin}));
		push(@TempPacket, _int_to_bytes(1,$event-&gt;{auth}));
		push(@TempPacket, _int_to_bytes(2,length($event-&gt;{reason})));
		push(@TempPacket, _str_to_bytes($event-&gt;{reason}));
		push(@{$Responce-&gt;{Data_Load}}, @TempPacket);
		push(@{$Me-&gt;{_Outgoing_Queue}}, $Responce);
		return $nRequestID;
	},
	#Cmd_Add_ContactList
	'19:20' =&gt; sub {
		my($Me, $event) = @_;
		my($Responce);

		@{$Responce-&gt;{Data_Load}} = &amp;_Make_SNAC_Header(19, 20, 0, 0, 20);

		#don't send the command unless we have a list to send..
		return if ($#{$event-&gt;{ContactList}} == -1);

		foreach (@{$event-&gt;{ContactList}}){
			push(@{$Responce-&gt;{Data_Load}}, _int_to_bytes(1, length($_)));
			push(@{$Responce-&gt;{Data_Load}}, _str_to_bytes($_));
		}
		push(@{$Responce-&gt;{Data_Load}}, _int_to_bytes(4,0x00000000));
		push(@{$Me-&gt;{_Outgoing_Queue}}, $Responce);
	},

	# Cmd_Register -- registers new UIN
	'23:4' =&gt; sub {
		my($Me, $event) = @_;
		my($Responce, @TempPacket);
		$Responce-&gt;{Channel_ID} = 2;
		@{$Responce-&gt;{Data_Load}} = &amp;_Make_SNAC_Header(0x17, 4, 0, 0);
		my $id = 0x9E270000;
		push(@{$Responce-&gt;{Data_Load}}, _int_to_bytes(3, 0x000100));
		push(@{$Responce-&gt;{Data_Load}}, _int_to_bytes(1, 0x36));
		push(@{$Responce-&gt;{Data_Load}}, _int_to_bytes(4, 0x0));
		push(@{$Responce-&gt;{Data_Load}}, _int_to_bytes(4, 0x28000300));
		push(@{$Responce-&gt;{Data_Load}}, _int_to_bytes(4, 0x0));
		push(@{$Responce-&gt;{Data_Load}}, _int_to_bytes(4, 0x0));
		push(@{$Responce-&gt;{Data_Load}}, _int_to_bytes(4, $id));
		push(@{$Responce-&gt;{Data_Load}}, _int_to_bytes(4, $id));
		push(@{$Responce-&gt;{Data_Load}}, _int_to_bytes(4, 0x0));
		push(@{$Responce-&gt;{Data_Load}}, _int_to_bytes(4, 0x0));
		push(@{$Responce-&gt;{Data_Load}}, _int_to_bytes(4, 0x0));
		push(@{$Responce-&gt;{Data_Load}}, _int_to_bytes(4, 0x0));
		push(@{$Responce-&gt;{Data_Load}}, _str_to_lnts($event-&gt;{'password'}));
		push(@{$Responce-&gt;{Data_Load}}, _int_to_bytes(4, $id));
		push(@{$Responce-&gt;{Data_Load}}, _int_to_bytes(4, 0x00000302));
		push(@{$Me-&gt;{_Outgoing_Queue}}, $Responce);
		# $Responce = {};
		# $Responce-&gt;{Channel_ID} = 4;
		# push(@{$Me-&gt;{_Outgoing_Queue}}, $Responce);
	},
	#Cmd_Srv_Message
	'21:2' =&gt; sub {
		my($Me, $event) = @_;
		my($Responce, @TempPacket);

		@{$Responce-&gt;{Data_Load}} = &amp;_Make_SNAC_Header(0x15, 2, 0, 0, ($Me-&gt;{_Mem}*65536+0x02)); #strange request ID..
		$Me-&gt;{_Mem}++;

		print STDERR "REF:".($Me-&gt;{_Mem})."\n";

		push(@{$Responce-&gt;{Data_Load}}, _int_to_bytes(2, 0x0001));

		# Argh, Finally figured this bit out!!!
		# this next four packets is the length in little endian
		# and normal!! so work out the packet length first...

		push(@TempPacket, _int_to_endian_bytes(4, $Me-&gt;{_UIN})); #encode the ICQ num..

		if ($event-&gt;{MessageType} eq "request_offline") {
			push(@TempPacket, _int_to_bytes(2, 0x3c00));
			push(@TempPacket, _int_to_bytes(1, $Me-&gt;{_Mem}));
			push(@TempPacket, _int_to_bytes(1, 0));
		} elsif ($event-&gt;{MessageType} eq "ack_offline") {
			push(@TempPacket, _int_to_bytes(2, 0x3e00));
			push(@TempPacket, _int_to_bytes(1, $Me-&gt;{_Mem}));
			push(@TempPacket, _int_to_bytes(1, 0));
		} elsif ($event-&gt;{MessageType} eq "key") {
			print "sending key [$event-&gt;{Key}]\n";

			$Me-&gt;{_Sent_Requests}{ (($Me-&gt;{_Mem}-1)*65536+0x02) } = $event-&gt;{Key};

			push(@TempPacket, _int_to_bytes(2, 0xd007));
			push(@TempPacket, _int_to_bytes(1, $Me-&gt;{_Mem}));
			push(@TempPacket, _int_to_bytes(3, 0x9808));

			my $Key = "&lt;key&gt;".$event-&gt;{Key}."&lt;/key&gt;";

			push(@TempPacket, _int_to_endian_bytes(2, length($Key)+1));
			push(@TempPacket, _str_to_bytes($Key));
			push(@TempPacket, _int_to_bytes(1, 0));

		} elsif ($event-&gt;{MessageType} eq "SMS") {
			push(@TempPacket, _int_to_bytes(2, 0xd007));
			push(@TempPacket, _int_to_bytes(1, $Me-&gt;{_Mem}));

			push(@TempPacket, _int_to_bytes(4, 0x00821400));
			push(@TempPacket, _int_to_bytes(4, 0x01001600));
			push(@TempPacket, _int_to_bytes(17, 0));

			my $TimeString = gmtime();
			if ($TimeString =~ /(\w+) (\w+)\s+(\d+) (\d+:\d+:\d+) (\d+)/) {
				$TimeString = $1.", ".$3." ".$2." ".$5." ".$4." GMT";
			} else {
				print "Unable to encode time...\n";
				return;
			}
			my $text = xml_escape($event-&gt;{text});
			my $SMSMessage = "&lt;icq_sms_message&gt;&lt;destination&gt;".$event-&gt;{SMS_Dest_Number}."&lt;/destination&gt;&lt;text&gt;".$text."&lt;/text&gt;";
			   $SMSMessage .= "&lt;codepage&gt;1252&lt;/codepage&gt;&lt;senders_UIN&gt;".$Me-&gt;{_UIN}."&lt;/senders_UIN&gt;&lt;senders_name&gt;Robbot&lt;/senders_name&gt;";
			   $SMSMessage .= "&lt;delivery_receipt&gt;Yes&lt;/delivery_receipt&gt;&lt;time&gt;$TimeString&lt;/time&gt;&lt;/icq_sms_message&gt;";

			my $SMSLength = length($SMSMessage)+1;

			push(@TempPacket, _int_to_bytes(2, $SMSLength));
			push(@TempPacket, _str_to_bytes($SMSMessage));
			push(@TempPacket, _int_to_bytes(1, 0)); #null end..

		} elsif ($event-&gt;{MessageType} eq "user_short_info_request") {
			push(@TempPacket, _int_to_bytes(2, 0xd007));
			push(@TempPacket, _int_to_endian_bytes(2, $Me-&gt;{_Mem}));
			push(@TempPacket, _int_to_bytes(2, 0xba04));
			push(@TempPacket, _int_to_endian_bytes(4, $event-&gt;{TargetUIN})); #encode the ICQ num..

		} elsif ($event-&gt;{MessageType} eq "Set_Permissions") {
			push(@TempPacket, _int_to_bytes(2, 0xd007));
			push(@TempPacket, _int_to_endian_bytes(2, $Me-&gt;{_Mem}));
			push(@TempPacket, _int_to_bytes(2, 0x2404));
			my $auth = $event-&gt;{_auth} ? 0 : 1;
			my $web = $event-&gt;{_web};
			push(@TempPacket, _int_to_endian_bytes(1, $auth)); #encode the ICQ num..
			push(@TempPacket, _int_to_endian_bytes(1, $web));
			push(@TempPacket, _int_to_bytes(2, 0x0100));

		} elsif ($event-&gt;{MessageType} eq "Get_WP_Info") {
			push(@TempPacket, _int_to_bytes(2, 0xd007));
			push(@TempPacket, _int_to_endian_bytes(2, $Me-&gt;{_Mem}));
			$Me-&gt;{_Requests}-&gt;{($Me-&gt;{_Mem}-1)*65536+0x02} = $event-&gt;{TargetUIN};
			push(@TempPacket, _int_to_bytes(2, 0xb204));
			push(@TempPacket, _int_to_endian_bytes(4, $event-&gt;{TargetUIN})); #encode the ICQ num..

		} elsif ($event-&gt;{MessageType} eq "Random_Search") {
			push(@TempPacket, _int_to_bytes(2, 0xd007));
			push(@TempPacket, _int_to_endian_bytes(2, $Me-&gt;{_Mem}));
			$Me-&gt;{_Requests}-&gt;{($Me-&gt;{_Mem}-1)*65536+0x02} = $event-&gt;{TargetUIN};
			push(@TempPacket, _int_to_bytes(2, 0x4e07));
			push(@TempPacket, _int_to_endian_bytes(1, $event-&gt;{TargetGroup}));
			push(@TempPacket, _int_to_endian_bytes(1, 0));

		} elsif ($event-&gt;{MessageType} eq "Set_Group") {
			push(@TempPacket, _int_to_bytes(2, 0xd007));
			push(@TempPacket, _int_to_endian_bytes(2, $Me-&gt;{_Mem}));
			$Me-&gt;{_Requests}-&gt;{($Me-&gt;{_Mem}-1)*65536+0x02} = $event-&gt;{TargetUIN};
			push(@TempPacket, _int_to_bytes(2, 0x5807));
			push(@TempPacket, _int_to_endian_bytes(1, $event-&gt;{TargetGroup}));
			push(@TempPacket, _int_to_bytes(4, 0x00000003));
			push(@TempPacket, _int_to_bytes(4, 0x20000000));
			push(@TempPacket, _int_to_bytes(4, 0x00000000));
			push(@TempPacket, _int_to_bytes(4, 0x00000000));
			push(@TempPacket, _int_to_bytes(4, 0x00040800));
			push(@TempPacket, _int_to_bytes(4, 0x00000000));
			push(@TempPacket, _int_to_bytes(4, 0x00005000));
			push(@TempPacket, _int_to_bytes(4, 0x00000300));
			push(@TempPacket, _int_to_bytes(2, 0x0000));

		} elsif ($event-&gt;{MessageType} eq "Change_Password") {
			push(@TempPacket, _int_to_bytes(2, 0xd007));
			push(@TempPacket, _int_to_endian_bytes(2, $Me-&gt;{_Mem}));
			$Me-&gt;{_Requests}-&gt;{($Me-&gt;{_Mem}-1)*65536+0x02} = $event-&gt;{TargetUIN};
			push(@TempPacket, _int_to_bytes(2, 0x2e04));
			push(@TempPacket, _str_to_lnts($event-&gt;{Password}));

		} elsif ($event-&gt;{MessageType} eq "Self_Info_Request") {
			push(@TempPacket, _int_to_bytes(2, 0xd007));
			push(@TempPacket, _int_to_endian_bytes(2, $Me-&gt;{_Mem}));
			push(@TempPacket, _int_to_bytes(2, 0xd004));
			push(@TempPacket, _int_to_endian_bytes(4, $Me-&gt;{_UIN})); #encode the ICQ num..
			# print "&gt;&gt; $Me-&gt;{_UIN}\n";

		} elsif ($event-&gt;{MessageType} eq "Set_Main_WP_Info") {
			push(@TempPacket, _int_to_bytes(2, 0xd007));
			push(@TempPacket, _int_to_endian_bytes(2, $Me-&gt;{_Mem}));
			push(@TempPacket, _int_to_bytes(2, 0xea03));
			push(@TempPacket, _str_to_lnts($event-&gt;{_nickname}));
			push(@TempPacket, _str_to_lnts($event-&gt;{_firstname}));
			push(@TempPacket, _str_to_lnts($event-&gt;{_lastname}));
			push(@TempPacket, _str_to_lnts($event-&gt;{_email}));
			push(@TempPacket, _str_to_lnts($event-&gt;{_city}));
			push(@TempPacket, _str_to_lnts($event-&gt;{_state}));
			push(@TempPacket, _str_to_lnts($event-&gt;{_phone}));
			push(@TempPacket, _str_to_lnts($event-&gt;{_fax}));
			push(@TempPacket, _str_to_lnts($event-&gt;{_street}));
			push(@TempPacket, _str_to_lnts($event-&gt;{_cellular}));
			push(@TempPacket, _str_to_lnts($event-&gt;{_zip}));
			push(@TempPacket, _int_to_endian_bytes(2,$event-&gt;{_country}));
			push(@TempPacket, _int_to_bytes(1,$event-&gt;{_GMT}));
			push(@TempPacket, _int_to_bytes(1,0));

		} elsif ($event-&gt;{MessageType} eq "User_Info_Request") {
			push(@TempPacket, _int_to_bytes(2, 0xd007));
			push(@TempPacket, _int_to_endian_bytes(2, $Me-&gt;{_Mem}));
			push(@TempPacket, _int_to_bytes(2, 0xb204));
			push(@TempPacket, _int_to_endian_bytes(4, $event-&gt;{TargetUIN})); #encode the ICQ num..

		} elsif ($event-&gt;{MessageType} eq "WP_Full_Request") {
			push(@TempPacket, _int_to_bytes(2, 0xd007));
			push(@TempPacket, _int_to_endian_bytes(2, $Me-&gt;{_Mem}));
			push(@TempPacket, _int_to_bytes(2, 0x3305));

		#max 20 on everything unless noted
		#first
			push(@TempPacket, _int_to_bytes(1, length($event-&gt;{_firstname})+1));
			push(@TempPacket, _int_to_bytes(1, 0x00));
			push(@TempPacket, _str_to_bytes($event-&gt;{_firstname}));
			push(@TempPacket, _int_to_bytes(1, 0x00));
		#last
			push(@TempPacket, _int_to_bytes(1, length($event-&gt;{_lastname})+1));
			push(@TempPacket, _int_to_bytes(1, 0x00));
			push(@TempPacket, _str_to_bytes($event-&gt;{_lastname}));
			push(@TempPacket, _int_to_bytes(1, 0x00));
		#nick
			push(@TempPacket, _int_to_bytes(1, length($event-&gt;{_nickname})+1));
			push(@TempPacket, _int_to_bytes(1, 0x00));
			push(@TempPacket, _str_to_bytes($event-&gt;{_nickname}));
			push(@TempPacket, _int_to_bytes(1, 0x00));
		#email (max 25)
			push(@TempPacket, _int_to_bytes(1, length($event-&gt;{_email})+1));
			push(@TempPacket, _int_to_bytes(1, 0x00));
			push(@TempPacket, _str_to_bytes($event-&gt;{_email}));
			push(@TempPacket, _int_to_bytes(1, 0x00));
		#Min age
			push(@TempPacket, _int_to_bytes(1, $event-&gt;{_min_age}));
			push(@TempPacket, _int_to_bytes(1, 0x00));
		#Max age
			push(@TempPacket, _int_to_bytes(1, $event-&gt;{_max_age}));
			push(@TempPacket, _int_to_bytes(1, 0x00));
		#Sex (0,1,2)
			push(@TempPacket, _int_to_bytes(1, $event-&gt;{_sex}));
		#Language (0...see table)
			push(@TempPacket, _int_to_bytes(1, $event-&gt;{_language}));
		#city
			push(@TempPacket, _int_to_bytes(1, length($event-&gt;{_city})+1));
			push(@TempPacket, _int_to_bytes(1, 0x00));
			push(@TempPacket, _str_to_bytes($event-&gt;{_city}));
			push(@TempPacket, _int_to_bytes(1, 0x00));
		#state (max 3)
			push(@TempPacket, _int_to_bytes(1, length($event-&gt;{_state})+1));
			push(@TempPacket, _int_to_bytes(1, 0x00));
			push(@TempPacket, _str_to_bytes($event-&gt;{_state}));
			push(@TempPacket, _int_to_bytes(1, 0x00));
		#country (see table)
			push(@TempPacket, _int_to_bytes(1, $event-&gt;{_country}));
			push(@TempPacket, _int_to_bytes(1, 0x00));
		#company-name
			push(@TempPacket, _int_to_bytes(1, length($event-&gt;{_company_name})+1));
			push(@TempPacket, _int_to_bytes(1, 0x00));
			push(@TempPacket, _str_to_bytes($event-&gt;{_company_name}));
			push(@TempPacket, _int_to_bytes(1, 0x00));
		#company-department
			push(@TempPacket, _int_to_bytes(1, length($event-&gt;{_company_dep})+1));
			push(@TempPacket, _int_to_bytes(1, 0x00));
			push(@TempPacket, _str_to_bytes($event-&gt;{_company_dep}));
			push(@TempPacket, _int_to_bytes(1, 0x00));
		#company-position
			push(@TempPacket, _int_to_bytes(1, length($event-&gt;{_company_pos})+1));
			push(@TempPacket, _int_to_bytes(1, 0x00));
			push(@TempPacket, _str_to_bytes($event-&gt;{_company_pos}));
			push(@TempPacket, _int_to_bytes(1, 0x00));
		#company-occupation
			push(@TempPacket, _int_to_bytes(1, $event-&gt;{_company_occ}));
		#past information category
			push(@TempPacket, _int_to_bytes(2, $event-&gt;{_past_info_cat}));
		#past information
			push(@TempPacket, _int_to_bytes(1, length($event-&gt;{_past_info_desc})+1));
			push(@TempPacket, _int_to_bytes(1, 0x00));
			push(@TempPacket, _str_to_bytes($event-&gt;{_past_info_desc}));
			push(@TempPacket, _int_to_bytes(1, 0x00));
		#interests category (see table)
			push(@TempPacket, _int_to_bytes(1, $event-&gt;{_interests_cat}));
			push(@TempPacket, _int_to_bytes(1, 0x00));
		#interests specific - comma, delim
			push(@TempPacket, _int_to_bytes(1, length($event-&gt;{_interests_desc})+1));
			push(@TempPacket, _int_to_bytes(1, 0x00));
			push(@TempPacket, _str_to_bytes($event-&gt;{_interests_desc}));
			push(@TempPacket, _int_to_bytes(1, 0x00));
		#organization
			push(@TempPacket, _int_to_bytes(1, $event-&gt;{_org_cat}));
			push(@TempPacket, _int_to_bytes(1, 0x00));
		#organization specific - comma, delim
			push(@TempPacket, _int_to_bytes(1, length($event-&gt;{_org_desc})+1));
			push(@TempPacket, _int_to_bytes(1, 0x00));
			push(@TempPacket, _str_to_bytes($event-&gt;{_org_desc}));
			push(@TempPacket, _int_to_bytes(1, 0x00));
		#homepage category
			push(@TempPacket, _int_to_bytes(2, $event-&gt;{_homepage_cat}));
		#homepage
			push(@TempPacket, _int_to_bytes(1, length($event-&gt;{_homepage})+1));
			push(@TempPacket, _int_to_bytes(1, 0x00));
			push(@TempPacket, _str_to_bytes($event-&gt;{_homepage}));
			push(@TempPacket, _int_to_bytes(1, 0x00));
		#Only online users (0 or 1)
			push(@TempPacket, _int_to_bytes(1, $event-&gt;{_online_only}));

		}

		#NOW work out that length thingy (what a crappy place for it!!!)
		push(@{$Responce-&gt;{Data_Load}}, _int_to_bytes(2, $#TempPacket + 3));
		push(@{$Responce-&gt;{Data_Load}}, _int_to_endian_bytes(2, $#TempPacket + 1));
		push(@{$Responce-&gt;{Data_Load}}, @TempPacket);
		push(@{$Me-&gt;{_Outgoing_Queue}}, $Responce);

	},
);

%_FileDC_Decoder = (
	'File:0' =&gt; sub {
		my @Response;
		my ($Me, $event) = @_;
		my $connection = $event-&gt;{Connection};
		my $i = 7;
		my $files = _endian_bytes_to_int($event-&gt;{Data_Load}, $i ,4);
		$i += 4;
		my $length = _endian_bytes_to_int($event-&gt;{Data_Load}, $i ,4);
		$i += 4;
		my $speed = _endian_bytes_to_int($event-&gt;{Data_Load}, $i ,4);
		$i += 4;
		my $name = _bytes_to_lnts($event-&gt;{Data_Load}, $i);
		$connection-&gt;{FileNum} = 0;
		push @Response,_int_to_endian_bytes(2, 11); # length
		push @Response,_int_to_endian_bytes(1, 0x1); # id
		push @Response,_int_to_endian_bytes(4, 0x00000064); # speed
		push @Response,_str_to_lnts("xxx"); # Nick
		push @{$connection-&gt;{ToSend}},@Response;
	},
	'File:5' =&gt; sub {
		# Change speed, just ignore this
	},
	'File:6' =&gt; sub {
		# Got Data!
		my ($Me, $event) = @_;
		my $dir = $Me-&gt;{_Incoming_Dir};
		if($dir ne '') {
			$dir .= '/';
		}
		my $connection = $event-&gt;{Connection};
		my $i = 0;
		my $size = _endian_bytes_to_int($event-&gt;{Data_Load}, $i, 2);
		$size--;
		my $data = _bytes_to_str($event-&gt;{Data_Load}, 1, $size);
		open FILE, "&gt;&gt; $dir" . "$connection-&gt;{FileName}";
		binmode FILE;
		print FILE $data;
		close FILE;
	},
	'File:2' =&gt; sub {
		my @Response;
		my $Refined;
		my ($Me, $event) = @_;
		my $connection = $event-&gt;{Connection};
		my $i = 4;
		my $name = _bytes_to_lnts($event-&gt;{Data_Load}, $i);
		$i += length($name) + 3;
		$connection-&gt;{FileName} = $name;
		$connection-&gt;{FileNum}++;
		my $text = _bytes_to_lnts($event-&gt;{Data_Load}, $i);
		$i += length($text) + 3;
		my $size = _endian_bytes_to_int($event-&gt;{Data_Load}, $i ,4);
		# TODO Make resume...
		my $num = $connection-&gt;{FileNum};

		$Refined-&gt;{MessageType} = "file";
		$Refined-&gt;{Connection} = $connection;
		$Refined-&gt;{Sender} = $connection-&gt;{UIN};

		push @Response,_int_to_endian_bytes(2, 17); # length
		push @Response,_int_to_endian_bytes(1, 0x3); # id
		push @Response,_int_to_endian_bytes(4, 0x00000000); # skip size
		push @Response,_int_to_endian_bytes(4, 0x00000000); # unkn
		push @Response,_int_to_endian_bytes(4, 0x00000064); # speed
		push @Response,_int_to_endian_bytes(4, $num); # unknown
		push @{$connection-&gt;{ToSend}},@Response;
		return $Refined;
	}
);

%_DC_Decoder = (
	'DC:2030' =&gt; sub {
		my ($Me, $event) = @_;
		my @Ack = ();
		my $skipack = 0;
		my $seqnum = _endian_bytes_to_int($event-&gt;{Data_Load},11 ,2); # seqn
		my $type = _endian_bytes_to_int($event-&gt;{Data_Load},25 ,1); # flags
		my $flags = _endian_bytes_to_int($event-&gt;{Data_Load},26 ,1); # flags
		my $connection = $event-&gt;{Connection};
		my $Refined;
		my $msg = '';
		$Refined-&gt;{MessageType} = "DC:$type";
		if($type == 1) {
			my $i = 31;
			my $DataLength = _endian_bytes_to_int($event-&gt;{Data_Load}, $i ,2);
			$i+=2;
			my $text = _bytes_to_str($event-&gt;{Data_Load},$i , $DataLength);
			$Refined-&gt;{text} = $text;
			$Refined-&gt;{Sender} = $connection-&gt;{UIN};
			$Refined-&gt;{MessageType} = "text_message";

		} elsif($type == 2) {
			my $i = 31;
			$Refined-&gt;{Reason} = _bytes_to_lnts($event-&gt;{Data_Load},$i);
			$Refined-&gt;{Sender} = $connection-&gt;{UIN};
			$Refined-&gt;{MessageType} = "chat";

		} elsif($type == 3) {
			my $i = 31;
			my $description = _bytes_to_lnts($event-&gt;{Data_Load},$i);
			$i += length($description) + 3;
			$i+=4;
			my $files = _bytes_to_lnts($event-&gt;{Data_Load},$i);
			$i += length($files) + 3;
			my $size = _endian_bytes_to_int($event-&gt;{Data_Load}, $i ,4);
			($Refined-&gt;{Description},$Refined-&gt;{Files},$Refined-&gt;{Size}) = ($description, $files, $size);
			$connection-&gt;{type} = $type;
			$connection-&gt;{flags} = $flags;
			$connection-&gt;{seqnum} = $seqnum;
			$Refined-&gt;{Sender} = $connection-&gt;{UIN};
			$Refined-&gt;{MessageType} = "files";
			$Refined-&gt;{Connection} = $connection;
			$skipack = 1;

			# This is generic decline ack... For future :)
			# @Ack = BuildGenericAck($type,$flags,$seqnum,1);
			# push(@Ack, _str_to_lnts("No files!"));
			# push(@Ack, _int_to_endian_bytes(4, 0xffffffff));
			# push(@Ack, _int_to_endian_bytes(3, 0x000100));
			# push(@Ack, _int_to_endian_bytes(4, 0xffffffff));
			# push(@Ack, _int_to_endian_bytes(4, 0xffffffff));

			# Accept ack
			# my $port = $Me-&gt;{_FilePort};
			# @Ack = BuildGenericAck($type,$flags,$seqnum);
			# push(@Ack, _int_to_endian_bytes(1, 0x01));
			# push(@Ack, _int_to_bytes(4, $port));
			# push(@Ack, _int_to_endian_bytes(2, 0x0000));
			# push(@Ack, _str_to_lnts(""));
			# push(@Ack, _int_to_bytes(4, 0x12345678));
			# push(@Ack, _int_to_endian_bytes(4, $port));

		} elsif($type == 4) {
			my $i = 31;
			my $DataLength = _endian_bytes_to_int($event-&gt;{Data_Load}, $i ,2);
			$i+=2;
			my $text = _bytes_to_str($event-&gt;{Data_Load},$i , $DataLength);
			($Refined-&gt;{Description},$Refined-&gt;{URL}) = split /\xfe/,$text;
			$Refined-&gt;{Sender} = $connection-&gt;{UIN};
			$Refined-&gt;{MessageType} = "URL";

		} elsif($type == 19) {
			my $i = 31;
			my $DataLength = _endian_bytes_to_int($event-&gt;{Data_Load}, $i ,2);
			$i+=2;
			my $text = _bytes_to_str($event-&gt;{Data_Load},$i , $DataLength);
			# ($Refined-&gt;{Description},$Refined-&gt;{URL}) = split /\xfe/,$text;
			my ($count,@contacts) = split /\xfe/,$text;
			pop @contacts;
			$Refined-&gt;{Count} = $count;
			$Refined-&gt;{Contacts} = \@contacts;
			$Refined-&gt;{Sender} = $connection-&gt;{UIN};
			$Refined-&gt;{MessageType} = "contacts";

		} elsif($type == 26) {
			my $i = 97;
			my $DataLength = _endian_bytes_to_int($event-&gt;{Data_Load}, $i ,4);
			$i+=4;
			my $text = _bytes_to_str($event-&gt;{Data_Load},$i , $DataLength);
			$Refined-&gt;{Sender} = $connection-&gt;{UIN};
			$Refined-&gt;{MessageType} = "contacts_request";
			$Refined-&gt;{Reason} = $text;

		} elsif($type == 0xE8) {
			# TODO Make normal messaging
			$msg = $Me-&gt;{_Away_Message}; # 'I am away!';
			$Refined-&gt;{MessageType} = "dummy";

		} elsif($type == 0xE9) {
			# TODO Make normal messaging
			$msg = $Me-&gt;{_Occupied_Message}; # 'Occupied!';
			$Refined-&gt;{MessageType} = "dummy";

		} elsif($type == 0xEA) {
			# TODO Make normal messaging
			$msg = $Me-&gt;{_NA_Message}; # 'Not available for this moment';
			$Refined-&gt;{MessageType} = "dummy";

		} elsif($type == 0xEB) {
			# TODO Make normal messaging
			$msg = $Me-&gt;{_DND_Message}; # 'Do Not Disturb!!!!';
			$Refined-&gt;{MessageType} = "dummy";

		} elsif($type == 0xEC) {
			# TODO Make normal messaging
			$msg = $Me-&gt;{_FFC_Message}; # 'Let\'s talk!';
			$Refined-&gt;{MessageType} = "dummy";

		} else {
			$Refined-&gt;{Sender} = $connection-&gt;{UIN};
			$Refined-&gt;{MessageType} = "DC:$type";
		}

		if(!@Ack) {
			@Ack = BuildGenericAck($type,$flags,$seqnum);
			push(@Ack, _str_to_lnts($msg));
			push(@Ack, _int_to_endian_bytes(4, 0x00000000));
			push(@Ack, _int_to_endian_bytes(4, 0xffffffff));
		}

		@Ack = Encrypt(\@Ack);
		push @{$connection-&gt;{ToSend}},@Ack unless $skipack;
		return $Refined;
	},
	'DC:2000' =&gt; sub {
			my $Refined;
			$Refined-&gt;{MessageType} = "dummy";
			return $Refined;
			# Cancel command
			# dummy :(
	}
);

%_Srv_Decoder = (
	#Srv_GSC_Ready
	'1:3' =&gt; sub {
		my ($Me, $event) = @_;
		#nothing interesting to get from SNAC..

		if ($Me-&gt;{_Auto_Login}) {
			$Me-&gt;Send_Command("Cmd_GSC_ICQInform");
		}
		return;
	},
	#Srv_GSC_Rate_Info
	"1:7" =&gt; sub {
		my ($Me, $event) = @_;
		#my ($Refined);

		if ($Me-&gt;{_Auto_Login} &gt; 1) {
			#ack the rate info..
			$Me-&gt;Send_Command("Cmd_GSC_Rate_Info_Ack");

			#also send some other requests..
			$Me-&gt;Send_Command("Cmd_GSC_LoggedIn_User_Info");
			$Me-&gt;Send_Command("Cmd_LS_LoggedIn_User_Rights");
			$Me-&gt;Send_Command("Cmd_BLM_Rights_Info");
			$Me-&gt;Send_Command("Cmd_Mes_Param_Info");
			$Me-&gt;Send_Command("Cmd_BOS_Get_Rights");
		}

		#Loads of data, but I have no idea what to do with it..
		#(tells us all the possible commands?..)
		return ($event);
	},
	#Srv_GSC_User_Info
	'1:15' =&gt; sub {
		my ($Me, $event) = @_;
		my ($Refined, $i, $DataLength);

		#$event-&gt;{Data_Load}

		$i = 10;

		$DataLength = ${$event-&gt;{Data_Load}}[$i];

		$i++;
		$Refined-&gt;{Online_User} = _bytes_to_str($event-&gt;{Data_Load}, $i, $DataLength);
		$i += $DataLength;
		$Refined-&gt;{Warning_Lev} = _bytes_to_int ($event-&gt;{Data_Load}, $i, 2);
		$i += 4;

		($Refined, $i) = &amp;_Read_TLV($event-&gt;{Data_Load}, 2, $i, $Refined);
		return ($Refined);
	},
	#Srv_GSC_MOTD
	'1:19' =&gt; sub {
		my ($Me, $event) = @_;
		my ($Refined, $i);

		$i = 12;

		($Refined, $i) = &amp;_Read_TLV($event-&gt;{Data_Load}, 2, $i, $Refined);
		return ($Refined);
	},
	#Srv_GSC_ICQClientConfirm
	'1:24' =&gt; sub {
		my ($Me, $event) = @_;
		my ($Refined);

		#$event-&gt;{Data_Load}

		if ($Me-&gt;{_Auto_Login}) {
			if ($Me-&gt;{_Auto_Login} == 1) {
				my ($details);

				$Me-&gt;Send_Command("Cmd_Mes_Add_ICBM_Param");
				$Me-&gt;Send_Command("Cmd_LS_Set_User_Info");
				$details-&gt;{Status} = $Me-&gt;{_Status};
				if(($Me-&gt;{_Status} &amp; 0x100 ) &amp;&amp; !$Me-&gt;{_no_lists}) {
					$Me-&gt;Send_Command("Cmd_BOS_Add_VisibleList",
						{ VisibleList=&gt; $Me-&gt;{_Auto_Login_Visible_List}});
				}
				if(!$Me-&gt;{_no_lists}) {
					$Me-&gt;Send_Command("Cmd_CTL_UploadList",
						{ContactList=&gt; $Me-&gt;{_Auto_Login_Contact_List}});
				}
				$Me-&gt;Send_Command("Cmd_GSC_Set_Status", $details);
				$Me-&gt;Send_Command("Cmd_GSC_Client_Ready");
				if(!($Me-&gt;{_Status} &amp; 0x100 ) &amp;&amp; !$Me-&gt;{_no_lists}) {
					$Me-&gt;Send_Command("Cmd_BOS_Add_InVisibleList",
						{ InVisibleList =&gt; $Me-&gt;{_Auto_Login_Invisible_List}});
				}
				$Me-&gt;Send_Command("Cmd_Srv_Message",
					{MessageType =&gt; "request_offline"})
				unless $Me-&gt;{_no_offline_messages};
				$Me-&gt;{_LoggedIn} = 1;
			} else {
				$Me-&gt;Send_Command("Cmd_GSC_Reqest_Rate_Info");
			}
		}
		return ($Refined);
	},
	#Srv_LS_Rights_Response
	'2:3' =&gt; sub {
		my ($Me, $event) = @_;
		my ($Refined);

		#no idea what to do with this data..
		#$event-&gt;{Data_Load}
		return ($Refined);
	},
	#Srv_BLM_Rights_Response
	'3:3' =&gt; sub {
		my ($Me, $event) = @_;
		my ($Refined);

		#no idea what to do with this data..
		#$event-&gt;{Data_Load}
		return ($Refined);
	},
	#Srv_BLM_Contact_Online
	'3:11' =&gt; sub {
		my ($Me, $event) = @_;
		my ($Refined, $DataLength, $i);

		$i = 10;
		$DataLength = ${$event-&gt;{Data_Load}}[$i];$i++;

		$Refined-&gt;{Sender} = _bytes_to_str($event-&gt;{Data_Load}, $i, $DataLength);$i += $DataLength + 4;

		($Refined, $i) = _Read_TLV($event-&gt;{Data_Load}, 2, $i, $Refined, _bytes_to_int($event-&gt;{Data_Load}, $i-4, 4));

		my $st = 0;
		$st = $Refined-&gt;{Status} if exists $Refined-&gt;{Status};
		$st &amp;= 0xffff;
	# my $hexst = sprintf '%04x',$st;
		$Refined-&gt;{MessageType} = "status_change";
		$Refined-&gt;{Status} = $st;
		# my $inv = 0;
		# if($st &amp; 0x100)
		# {
			# $hexst = sprintf '%04x',$st &amp; ~0x100;
			# $inv = 1;
		# }
		# if(exists $_r_Status_Codes{$hexst})
		# {
			# $Refined-&gt;{Status} = $_r_Status_Codes{$hexst};
			# if($inv)
			# {
				# $Refined-&gt;{Status} .= "[inv]";
			# }
		# } else
		# {
			# print "&gt;&gt; Unknown status: $hexst, please report this bug to gonzo\@ukrweb.net &lt;&lt;\n";
			# $Refined-&gt;{Status} = 'Online';
		# }
		if($Refined-&gt;{'LANInfo'}) {
			my @data = split //,$Refined-&gt;{'LANInfo'};
			foreach (@data)
			{
				$_ = ord;
			}
			$Refined-&gt;{'LAN_IP'} = inet_ntoa(_bytes_to_str(\@data,0,4));
			$Refined-&gt;{'LAN_Port'} = _bytes_to_int(\@data,4,4);
			$Refined-&gt;{'ConnectionType'} = _bytes_to_int(\@data,8,1);
			$Refined-&gt;{'ICQ_Version'} = _bytes_to_int(\@data,9,2);
			undef $Refined-&gt;{'LANInfo'};
		}
		return ($Refined);
	},
	#Srv_BLM_Contact_Offline
	'3:12' =&gt; sub {
		my ($Me, $event) = @_;
		my ($Refined, $DataLength, $i);

		$i = 10;
		$DataLength = ${$event-&gt;{Data_Load}}[$i];$i++;

		$Refined-&gt;{MessageType} = 'status_change';
		$Refined-&gt;{Sender} = _bytes_to_str($event-&gt;{Data_Load}, $i, $DataLength);$i += $DataLength + 4;
		$Refined-&gt;{Status} = 0xffff;

		($Refined, $i) = _Read_TLV($event-&gt;{Data_Load}, 2, $i, $Refined, _bytes_to_int($event-&gt;{Data_Load}, $i-4, 4));
		return ($Refined);
	},
	#Srv_Mes_Rights_Response
	'4:5' =&gt; sub {
		my ($Me, $event) = @_;
		my ($Refined);

		#no idea what to do with this data..
		#$event-&gt;{Data_Load}
		return ($Refined);
	},
	#Srv_Mes_Received
	'4:7' =&gt; sub {
		my ($Me, $event) = @_;
		my ($Refined, $i, $DataLength, $DataType);

		print "Incoming..\n" if $Me-&gt;{_Debug};
		_print_packet($event-&gt;{Data_Load}, ()) if $Me-&gt;{_Debug};

		$i = 19;
		my $fb = _bytes_to_str($event-&gt;{Data_Load}, 10, 10);

		$Refined-&gt;{SenderType} = $event-&gt;{Data_Load}-&gt;[$i];$i++;
		$DataLength = ${$event-&gt;{Data_Load}}[$i];$i++;
		$Refined-&gt;{Sender} = _bytes_to_str($event-&gt;{Data_Load}, $i, $DataLength);$i += $DataLength + 4;

		$Refined-&gt;{MessageType} = "unknown:" . $Refined-&gt;{SenderType};
		($Refined, $i) = _Read_TLV($event-&gt;{Data_Load}, 2, $i, $Refined, _bytes_to_int($event-&gt;{Data_Load}, $i-4, 4));

		if($Refined-&gt;{SenderType} == 1) {
			if(!$Refined-&gt;{Encoded_Message}) {
				my $ArrayLength = @{$event-&gt;{Data_Load}};
				while($i&lt;$ArrayLength) {
					#read in the Data Type/length..
					my $tlvType   = _bytes_to_int ($event-&gt;{Data_Load}, $i, 2);$i+=2;
					my $tlvLength = _bytes_to_int ($event-&gt;{Data_Load}, $i, 2);$i+=2;

					printf("---------TVL Type: %x, Length: %x----------\n",$tlvType,$tlvLength) if $Me-&gt;{_Debug};

					if($tlvType == 0x0002) { # message data
						$Refined-&gt;{MessageType} = "text_message";
						my $start_pos = $i;
						while($i-$start_pos &lt;= $tlvLength) {
							my $featuresType = _bytes_to_int ($event-&gt;{Data_Load}, $i, 2);$i+=2;
							my $featuresLength = _bytes_to_int ($event-&gt;{Data_Load}, $i, 2);$i+=2;

							printf("---------TVL Features Type: %x, Length: %x----------\n",$featuresType,$featuresLength) if $Me-&gt;{_Debug};

							if($featuresType eq 0x0501) { # required capabilities
								$Refined-&gt;{req_caps} = _bytes_to_int ($event-&gt;{Data_Load}, $i, $featuresLength);
								$i+=$featuresLength;
							}
							elsif($featuresType == 0x0101) { # message text
								$Refined-&gt;{charset_number} = _bytes_to_int ($event-&gt;{Data_Load}, $i, 2);$i+=2;
								$Refined-&gt;{charset_subset} = _bytes_to_int ($event-&gt;{Data_Load}, $i, 2);$i+=2;

								$Refined-&gt;{text} = _bytes_to_str($event-&gt;{Data_Load}, $i, $featuresLength-4);
								$Refined-&gt;{text} =~ s/\r\n/\n/g;
								$i+=($featuresLength-4);
							}
							elsif($featuresType == 0x000b) { # non direct connection notification
								$i+=$featuresLength;
							}
							else { # uknown feature
								printf("SNAC(4:7): Unkown TVL Feature Type: %x----------\n",$featuresType) if $Me-&gt;{_Debug};
								$i+=$featuresLength;
							}
						}
					}
					else {
						printf("SNAC(4:7): Unkown TVL Type: %x----------\n",$tlvType) if $Me-&gt;{_Debug};
						$i+=$tlvLength;
					}
				}
			}
			else { #this is a weird ass message, so decode it..
				my @Encoded_Message = split(/ /, $Refined-&gt;{Encoded_Message});
				undef $Refined-&gt;{Encoded_Message};
				$Refined-&gt;{Sender} = _endian_bytes_to_int(\@Encoded_Message,0,4);
				my $subtype = _bytes_to_int(\@Encoded_Message,4,1);
				my $flags = _bytes_to_int(\@Encoded_Message,5,1);
				my $DataLength = _endian_bytes_to_int(\@Encoded_Message,6,2);
				my $data = _bytes_to_str(\@Encoded_Message,8,$DataLength);
				$Refined-&gt;{MessageType} .= ":$subtype";
				if($subtype == 0x0E) {
					$Refined-&gt;{MessageType} = 'email_message';
					my ($name,$junk1,$junk2,$email,$junk3,$text) = split /\xfe/,$data;
					$Refined-&gt;{Name} = $name;
					$Refined-&gt;{Email} = $email;
					$Refined-&gt;{Text} = $text;
				} else {
					$Refined-&gt;{TaggedDataString} = _bytes_to_str(\@Encoded_Message, 0x32, _endian_bytes_to_int(\@Encoded_Message, 0x2f, 2));

					_Decode_Tagged_Text($Refined-&gt;{TaggedDataString}, $Refined);
				}
			}
			return ($Refined);
		}
		elsif($Refined-&gt;{SenderType} == 2)
		{
			$i += 4;
			my $flag = _bytes_to_int($event-&gt;{Data_Load}, $i, 2);
			$i += 2;
			$i += 24;
			while($i&lt;@{$event-&gt;{Data_Load}})
			{
				my $tlv_type = _bytes_to_int($event-&gt;{Data_Load}, $i, 2);
				$i += 2;
				my $tlv_length = _bytes_to_int($event-&gt;{Data_Load}, $i, 2);
				$i += 2;
				my $tlvdata = _bytes_to_str($event-&gt;{Data_Load}, $i, $tlv_length);
				# print "?? $tlv_type : $tlv_length ??\n";
				my $oldi = $i;
				my $reply = "";
				# Let's ignore misc info packets
				$Refined-&gt;{MessageType} = "dummy";

				if($tlv_type == 0x2711)
				{
					my $len = _endian_bytes_to_int($event-&gt;{Data_Load}, $i, 2);
					$event-&gt;{Data_Load}[$i+2+24] = 0;
					$event-&gt;{Data_Load}[$i+2] = 7; # Protocol version
					my $chunk1 = _bytes_to_str($event-&gt;{Data_Load}, $i, $len+2);
					$i += 2+$len; #ignore files part
					$len = _endian_bytes_to_int($event-&gt;{Data_Load}, $i, 2);
					if($len == 0x0e)
					{
						my $chunk2 = _bytes_to_str($event-&gt;{Data_Load}, $i, $len+2);
						$i += 2+$len; #ignoring chunk 2

						my $type = _bytes_to_int($event-&gt;{Data_Load}, $i, 1);
						$i++;
						my $flags = _bytes_to_int($event-&gt;{Data_Load}, $i, 1);
						$i++;
						my $status_code = _bytes_to_int($event-&gt;{Data_Load}, $i, 2);
						$i += 2;
						$i += 2; # priority
						$len = _endian_bytes_to_int($event-&gt;{Data_Load}, $i, 2);
						$i += 2;
						my $msg = _bytes_to_str($event-&gt;{Data_Load}, $i, $len);
						$i += $len;
						$Refined-&gt;{MessageType} = $type;
						# print "Message: $type:$flags $status_code\n\n";
						if($type == 0) # Strange message
						{
							$Refined-&gt;{MessageType} = "dummy"; # ignore it
						}
						if($type == 1) # text message
						{
							$Refined-&gt;{MessageType} = "text_message";
							$Refined-&gt;{text} = $msg;
						} elsif($type == 2) # Chat
						{
							$Refined-&gt;{MessageType} = "chat";
							$Refined-&gt;{Reason} = $msg;
						} elsif($type == 3) # File ops
						{
							$Refined-&gt;{MessageType} = "file_request";
							# print "debug:: $msg\n\n";
						} elsif($type == 4) # URL
						{
							$Refined-&gt;{MessageType} = "URL";
							($Refined-&gt;{Description},$Refined-&gt;{URL}) = split /\xfe/,$msg;

						} elsif($type == 6) # Auth request
						{
							my($nick,$fname,$lname,$email,$xxx,$reason) = split /\xfe/,$msg;;
							$Refined-&gt;{nick} = $nick;
							$Refined-&gt;{first_name} = $fname;
							$Refined-&gt;{last_name} = $lname;
							$Refined-&gt;{email} = $email;
							$Refined-&gt;{reason} = $reason;
							$Refined-&gt;{MessageType} = "auth_request";
						} elsif($type == 0xC) # You have been added
						{
							$Refined-&gt;{MessageType} = "add_message";
						} elsif($type == 0xD) # WEB-pager
						{
							$Refined-&gt;{MessageType} = "web_message";
							my ($name,$junk,$junk2,$email,$junk3,$ip,$text) = split /\xfe/,$msg;
							$Refined-&gt;{Name} = $name;
							$Refined-&gt;{Email} = $email;
							$Refined-&gt;{Text} = $text;
							$Refined-&gt;{SenderIP} = $ip;
						} elsif($type == 0xE) # Email
						{
							$Refined-&gt;{MessageType} = 'email_message';
							my ($name,$junk,$junk2,$email,$junk3,$text) = split /\xfe/,$msg;
							$Refined-&gt;{Name} = $name;
							$Refined-&gt;{Email} = $email;
							$Refined-&gt;{Text} = $text;
						} elsif($type == 0x13) # Contacts
						{
							my ($contactcount,@contacts) = split /\xfe/,$msg;
							$Refined-&gt;{MessageType} = "contacts";
							$Refined-&gt;{Count} = $contactcount;
							$Refined-&gt;{Contacts} = \@contacts;
						} elsif($type == 0x1A) # Contacts req || GCard
						{
							my $id = _endian_bytes_to_int($event-&gt;{Data_Load}, $i, 1);
							$i++;
							if($id == 57) # Contacts request
							{
								$i += 0x3e;
								my $len = _endian_bytes_to_int($event-&gt;{Data_Load}, $i, 4);
								$i += 4;
								$Refined-&gt;{MessageType} = "contacts_request";
								$Refined-&gt;{Reason} = _bytes_to_str($event-&gt;{Data_Load}, $i, $len);
							} else { # Greeting card
								$i += 19;
								my ($junk, $version, $theme, $pre_title, $design);
								my ( $title, $recipient, $text, $sender, $url);
								($junk, $i) = _bytes_to_ds($event-&gt;{Data_Load}, $i);
								$i += 3;
								my $minor = _endian_bytes_to_int($event-&gt;{Data_Load}, $i, 2);
								$i += 2;
								my $major = _endian_bytes_to_int($event-&gt;{Data_Load}, $i, 2);
								$i += 2;
								$version = "$major.$minor";
								if($version eq "1.2")
								{
									$i += 12;
								} else
								{
									$i += 4;
								}
								($version, $i) = _bytes_to_ds($event-&gt;{Data_Load}, $i);
								($theme, $i) = _bytes_to_ds($event-&gt;{Data_Load}, $i);
								($pre_title, $i) = _bytes_to_ds($event-&gt;{Data_Load}, $i);
								($design, $i) = _bytes_to_ds($event-&gt;{Data_Load}, $i);
								($title, $i) = _bytes_to_ds($event-&gt;{Data_Load}, $i);
								($recipient, $i) = _bytes_to_ds($event-&gt;{Data_Load}, $i);
								($text, $i) = _bytes_to_ds($event-&gt;{Data_Load}, $i);
								($sender, $i) = _bytes_to_ds($event-&gt;{Data_Load}, $i);
								$i += 4;
								if($version eq "1.2")
								{
									($url, $i) = _bytes_to_ds($event-&gt;{Data_Load}, $i);
								}
								$Refined-&gt;{MessageType} = "greeting_card";
								$Refined-&gt;{version} = $version;
								$Refined-&gt;{theme} = $theme;
								$Refined-&gt;{pre_title} = $pre_title;
								$Refined-&gt;{title} = $title;
								$Refined-&gt;{recipient} = $recipient;
								$Refined-&gt;{sender} = $sender;
								$Refined-&gt;{text} = $text;
								$Refined-&gt;{url} = $url;



							}
						} elsif($type == 0xE8) {
							# TODO Make normal messaging
							$reply = $Me-&gt;{_Away_Message}; # 'I am away!';
							$Refined-&gt;{MessageType} = "dummy";

						} elsif($type == 0xE9) {
							# TODO Make normal messaging
							$reply = $Me-&gt;{_Occupied_Message}; # 'Occupied!';
							$Refined-&gt;{MessageType} = "dummy";

						} elsif($type == 0xEA) {
							# TODO Make normal messaging
							$reply = $Me-&gt;{_NA_Message}; # 'Not available for this moment';
							$Refined-&gt;{MessageType} = "dummy";

						} elsif($type == 0xEB) {
							# TODO Make normal messaging
							$reply = $Me-&gt;{_DND_Message}; # 'Do Not Disturb!!!!';
							$Refined-&gt;{MessageType} = "dummy";

						} elsif($type == 0xEC) {
							# TODO Make normal messaging
							$reply = $Me-&gt;{_FFC_Message}; # 'Let\'s talk!';
							$Refined-&gt;{MessageType} = "dummy";

						}
						$Me-&gt;Send_Command("Cmd_Type2_Ack", {
							type =&gt; $type,
							flags =&gt; $flags,
							chunk1 =&gt; $chunk1,
							chunk2 =&gt; $chunk2,
							first_bytes =&gt; $fb,
							UIN =&gt; $Refined-&gt;{Sender},
							msg =&gt; $reply
						});
					} else
					{
						$Refined-&gt;{MessageType} = "dummy";
					}
				}
				$i = $oldi + $tlv_length;
			}
		}
		elsif($Refined-&gt;{SenderType} == 4)
		{
			$i += 8; # UIN
			my $type = _bytes_to_int($event-&gt;{Data_Load}, $i , 1); $i++;
			my $flags = _bytes_to_int($event-&gt;{Data_Load}, $i , 1); $i++;
			my $len = _endian_bytes_to_int($event-&gt;{Data_Load}, $i , 2); $i+=2;
			my $msg = _bytes_to_str($event-&gt;{Data_Load}, $i , $len);
			# print "&gt;&gt;&gt; $type:$flags:$len:$msg \n\n";
			if($type == 0) # Strange message
			{
				$Refined-&gt;{MessageType} = "dummy"; # ignore it
			}
			if($type == 1) # text message
			{
				$Refined-&gt;{MessageType} = "text_message";
				$Refined-&gt;{text} = $msg;
			} elsif($type == 4) # URL
			{
				$Refined-&gt;{MessageType} = "URL";
				($Refined-&gt;{Description},$Refined-&gt;{URL}) = split /\xfe/,$msg;
			} elsif($type == 6) # Auth request
			{
				my($nick,$fname,$lname,$email,$xxx,$reason) = split /\xfe/,$msg;;
				$Refined-&gt;{nick} = $nick;
				$Refined-&gt;{first_name} = $fname;
				$Refined-&gt;{last_name} = $lname;
				$Refined-&gt;{email} = $email;
				$Refined-&gt;{reason} = $reason;
				$Refined-&gt;{MessageType} = "auth_request";
			} elsif($type == 0xC) # You have been added
			{
				$Refined-&gt;{MessageType} = "add_message";
			} elsif($type == 0x13) # Contacts
			{
				my ($contactcount,@contacts) = split /\xfe/,$msg;
				$Refined-&gt;{MessageType} = "contacts";
				$Refined-&gt;{Count} = $contactcount;
				$Refined-&gt;{Contacts} = \@contacts;
			}
		}

		return ($Refined);
	},
	#Srv_Usr_Typing
	'4:20' =&gt; sub {
		##################################################
		# To receive this packet your client should send first 4:2 packet
		# http://iserverd1.khstu.ru/oscar/snac_04_02.html
		#
		##################################################
		my ($Me, $event) = @_;
		my ($Refined, $i, $DataLength);

		if($Me-&gt;{_Debug}) {
			print "Incoming..\n" ;
			_print_packet($event-&gt;{Data_Load}, ());
		}
		$i = 18;
		$Refined-&gt;{Channel_id} = _bytes_to_str($event-&gt;{Data_Load}, $i, 2);$i+=2;
		$DataLength = ${$event-&gt;{Data_Load}}[$i];$i++;
		$Refined-&gt;{Sender} = _bytes_to_str($event-&gt;{Data_Load}, $i, $DataLength);$i+=$DataLength;
		$Refined-&gt;{NotificationType} = _bytes_to_int($event-&gt;{Data_Load}, $i, 2);

		# $Refined-&gt;{NotificationType} can be:
		# 	0x0000 - typing finished sign
		# 	0x0001 - text typed sign
		# 	0x0002 - typing begun sign
		return ($Refined);
	},
	# Srv_Registration_Failed
	'23:1' =&gt; sub {
		my ($Me, $event) = @_;
		my ($Refined);
		$Refined-&gt;{ErrCode} =  _bytes_to_int($event-&gt;{Data_Load}, 10, 2);
		$Me-&gt;SetError("Request for registration rejected with code: ".$Refined-&gt;{ErrCode}."!");
		return ($Refined);
	},
	# Srv_Registered_UIN
	'23:5' =&gt; sub {
		my ($Me, $event) = @_;
		$Me-&gt;{_NewUIN} = _endian_bytes_to_int($event-&gt;{Data_Load}, 56 , 4);
	},
	#Srv_BOS_Rights
	'9:3' =&gt; sub {
		my ($Me, $event) = @_;
		my ($Refined);

		if ($Me-&gt;{_Auto_Login} &gt; 1) {
			$Me-&gt;Send_Command("Cmd_Mes_Add_ICBM_Param");
			$Me-&gt;Send_Command("Cmd_LS_Set_User_Info");
			$Me-&gt;Send_Command("Cmd_CTL_UploadList", {ContactList=&gt; $Me-&gt;{_Auto_Login_Contact_List}});
			$Me-&gt;Send_Command("Cmd_GSC_Set_Status", {Status =&gt; $Me-&gt;{_Status}});
			$Me-&gt;Send_Command("Cmd_GSC_Client_Ready");
			# now send all the Ad requests (hey, this is how the
			# client does it.. : /
			$Me-&gt;Send_Command("Cmd_Srv_Message", {MessageType =&gt; "request_offline"});
			$Me-&gt;Send_Command("Cmd_Srv_Message", {MessageType =&gt; "key", Key =&gt; "DataFilesIP"});
			$Me-&gt;Send_Command("Cmd_Srv_Message", {MessageType =&gt; "key", Key =&gt; "BannersIP"});
			$Me-&gt;Send_Command("Cmd_Srv_Message", {MessageType =&gt; "key", Key =&gt; "ChannelsIP"});

		}
		#$event-&gt;{Data_Load}
		return ($Refined);
	},
	# Srv_Contact_List
	'19:6' =&gt; sub {
		my ($Me, $event) = @_;
		my ($Refined, $DataLength);
		my $i=4;

		_print_packet($event-&gt;{Data_Load}, ()) if($Me-&gt;{_Debug});

		$Refined-&gt;{Flags} = _bytes_to_int($event-&gt;{Data_Load}, $i, 2);$i+=2;
		$Refined-&gt;{Request_ID} = _bytes_to_int($event-&gt;{Data_Load}, $i, 4);$i+=4;
		$i+=1; # pass version of SSI protocol
		my $nNumberOfItems = _bytes_to_int($event-&gt;{Data_Load}, $i, 2);$i+=2;

		while($nNumberOfItems&gt;0) { # process every item
			my $nLen = _bytes_to_int($event-&gt;{Data_Load}, $i, 2);$i+=2;
			my $sName = "";
			if($nLen) {
				$sName = _bytes_to_str($event-&gt;{Data_Load}, $i, $nLen);
				$i += $nLen;
			}
			my $nGroupID = _bytes_to_int($event-&gt;{Data_Load}, $i, 2);$i+=2;
			my $nItemID  = _bytes_to_int($event-&gt;{Data_Load}, $i, 2);$i+=2;
			my $nType    = _bytes_to_int($event-&gt;{Data_Load}, $i, 2);$i+=2;
			my $nLengthOfFollowingData = _bytes_to_int($event-&gt;{Data_Load}, $i, 2);$i+=2;

			print "(L:$nLen,N:$sName,GID: $nGroupID,IID: $nItemID,T: $nType,LTLV: $nLengthOfFollowingData)\n" if($Me-&gt;{_Debug});;

			while($nLengthOfFollowingData&gt;0) {
				my $sTLVType = 0xff;
				my $nTLVLenght = 0;
				my $sTLVValue = "";

				$sTLVType = _bytes_to_int($event-&gt;{Data_Load}, $i, 2);$i+=2;
				$nLengthOfFollowingData-=2;
				$nTLVLenght = _bytes_to_int($event-&gt;{Data_Load}, $i, 2);$i+=2;
				$nLengthOfFollowingData-=2;
				if($nTLVLenght) {
					$sTLVValue = _bytes_to_str($event-&gt;{Data_Load}, $i, $nTLVLenght);
				}
				$i+= $nTLVLenght;
				$nLengthOfFollowingData-=$nTLVLenght;

				# switch from different TLV types
				if($sTLVType == 0x0066) { # buddy awaits authorization
					$Refined-&gt;{Users}-&gt;{$sName}-&gt;{waiting} = 1;
				}
				elsif($sTLVType == 0x00c8) { # list of subitems for groups
				}
				elsif($sTLVType == 0x0131) { # the name in the contact item
					$Refined-&gt;{Users}-&gt;{$sName}-&gt;{name} = $sTLVValue;
				}
				elsif($sTLVType == 0x013a) { # SMS number for buddy
					$Refined-&gt;{Users}-&gt;{$sName}-&gt;{sms} = $sTLVValue;
				}
				elsif($sTLVType == 0x013c) { # Buddy Comment
					$Refined-&gt;{Users}-&gt;{$sName}-&gt;{comment} = $sTLVValue;
				}
				elsif($sTLVType == 0x00D4) { # uploaded for first time - time in UNIX timestamp
				}
				elsif($sTLVType ==  0x0145) { # last update
				}
				elsif($sTLVType ==  0x00ca) { # AIM privacy type
# 					If 1, then allow all users to see you.
# 					If 2, then block all users from seeing you.
# 					If 3, then allow only the users in the permit list.
# 					If 4, then block only the users in the deny list.
# 					If 5, then allow only users on your buddy list.
				}
			}

			if($nType == 0x0000) { # buddy record
				$Refined-&gt;{Users}-&gt;{$sName}-&gt;{pos} = $nItemID;
				push(@{$Refined-&gt;{Groups}-&gt;{$nGroupID}-&gt;{uins}},$sName);
			}
			elsif($nType == 0x0001) { # group record
				if($nGroupID) {
					$Refined-&gt;{Groups}-&gt;{$nGroupID}-&gt;{name} = $sName;
					$Refined-&gt;{Groups}-&gt;{$nGroupID}-&gt;{pos}  = $nItemID;
				}
				elsif($nGroupID==0) {
					# This is Meta Group -- skip info in it for now
				}
			}
			elsif($nType == 0x0002) { # permit record
				print "NET::vICQ: Srv_Contact_List() permit record\n" if($Me-&gt;{_Debug});
			}
			elsif($nType == 0x0003) { # deny record
				print "NET::vICQ: Srv_Contact_List() deny record\n" if($Me-&gt;{_Debug});
			}
			elsif($nType == 0x0004) { # permit/deny settings
				print "NET::vICQ: Srv_Contact_List() permit/deny settings\n" if($Me-&gt;{_Debug});
			}
			elsif($nType == 0x0005) { # presense info
				print "NET::vICQ: Srv_Contact_List() presense info\n" if($Me-&gt;{_Debug});
			}
			elsif($nType == 0x000e) { # ignore list record
				print "NET::vICQ: Srv_Contact_List() ignore list record\n" if($Me-&gt;{_Debug});
			}
			elsif($nType == 0x000f) { # last update time
				print "NET::vICQ: Srv_Contact_List() last update time\n" if($Me-&gt;{_Debug});
			}
			elsif($nType == 0x0010) { # non ICQ contact
				print "NET::vICQ: Srv_Contact_List() non ICQ contact\n" if($Me-&gt;{_Debug});
			}
			elsif($nType == 0x0013) { # import time
				print "NET::vICQ: Srv_Contact_List() import time\n" if($Me-&gt;{_Debug});
			}
			elsif($nType == 0x0014) { # own icon
				print "NET::vICQ: Srv_Contact_List() own icon\n" if($Me-&gt;{_Debug});
			}
			else {
				print "NET::vICQ: Srv_Contact_List() Unknown Item Type\n" if($Me-&gt;{_Debug});
			}
			$nNumberOfItems--;
		}
		if($Refined-&gt;{Flags}==0) {
			$Refined-&gt;{LastUpdate} = _bytes_to_int($event-&gt;{Data_Load}, $i, 4);$i+=4;
		}
		return ($Refined);
	},
	# Srv_List_Ack
	'19:14'=&gt; sub {
		my ($Me, $event) = @_;
		my ($Refined, $i);
		if($Me-&gt;{_Debug}) {
			print "Srv_List_Ack(): [".$event-&gt;{Channel_ID}."][".$event-&gt;{Sequence_ID}."][".$event-&gt;{Data_Size}."][".$event-&gt;{Family_ID}."][".$event-&gt;{Sub_ID}."]\n";
			_print_packet($event-&gt;{Data_Load}, ());
		}

		$Refined-&gt;{Request_ID} = _bytes_to_int($event-&gt;{Data_Load}, 6, 4);
		$Refined-&gt;{ErrCode} = _bytes_to_int($event-&gt;{Data_Load}, 10, 2);

		### Error Codes ###
		#   0x0000 	  No errors (success)
		#   0x0002 	  Item you want to modify not found in list
		#   0x0003 	  Item you want to add allready exists
		#   0x000A 	  Error adding item (invalid id, allready in list, invalid data)
		#   0x000C 	  Can't add item. Limit for this type of items exceeded
		#   0x000D 	  Trying to add ICQ contact to an AIM list
		#   0x000E 	  Can't add this contact because it requires authorization
		return ($Refined);
	},

	# Srv_Auth_Ack
	'19:27'=&gt; sub {
		my ($Me, $event) = @_;
		my ($Refined);
# 		if($Me-&gt;{_Debug}) {
		if(1) {
			_print_packet($event-&gt;{Data_Load}, ());
		}
		my $i=4;
		$Refined-&gt;{Flags} = _bytes_to_int($event-&gt;{Data_Load}, $i, 2);$i+=2;
		$Refined-&gt;{Request_ID} = _bytes_to_int($event-&gt;{Data_Load}, 6, 4);$i+=4;
		my $nDataLen = 0;
		if($Refined-&gt;{Flags}==0x8000) { # additional unknown data
			$nDataLen = _bytes_to_int($event-&gt;{Data_Load}, $i, 2);$i+=2;
			$i+= $nDataLen;	# skip additional data
		}
		$nDataLen = _bytes_to_int($event-&gt;{Data_Load}, $i, 2);$i+=2;
		$Refined-&gt;{UIN}= _bytes_to_int($event-&gt;{Data_Load}, $i, $nDataLen);$i+=$nDataLen;
		$Refined-&gt;{RetCode} = ${$event-&gt;{Data_Load}}[$i];$i++;
		$nDataLen = _bytes_to_int($event-&gt;{Data_Load}, $i, 2);$i+=2;
		$Refined-&gt;{RetText} = _bytes_to_str($event-&gt;{Data_Load}, $i,$nDataLen);

		return ($Refined);
	},
	# Srv_You_Were_Added
	'19:28'=&gt; sub {
		my ($Me, $event) = @_;
		my ($Refined);
		my $i = 10;
		my $nDataLen = _bytes_to_int($event-&gt;{Data_Load}, $i, 2);$i+=2;
		$Refined-&gt;{uin} =  _bytes_to_str($event-&gt;{Data_Load}, $i,$nDataLen);
		return ($Refined);
	},
	#Srv_Srv_Message
	'21:3' =&gt; sub {
		my ($Me, $event) = @_;
		my ($Refined, $i);

################
##### NOTE #####
################
# This Srv response seems to be the one that AOL decided to hack ALL ICQ
# functions that they couldn't fit under the normal AIM protocol. This means
# that this family seems to ave a lot of sub sub sub families, and hence is
# a bastard to decode, and then when u think u've got it, one call out of
# 900000 screws up in the decoding so if anyone has some good insights into
# this family please let me know!!!!

		print "Incoming..\n" if $Me-&gt;{_Debug};
		print "[".$event-&gt;{Channel_ID}."][".$event-&gt;{Sequence_ID}."][".$event-&gt;{Data_Size}."][".$event-&gt;{Family_ID}."][".$event-&gt;{Sub_ID}."]\n" if $Me-&gt;{_Debug};
		_print_packet($event-&gt;{Data_Load}, ()) if $Me-&gt;{_Debug};

		$Refined-&gt;{Flags} = _bytes_to_int($event-&gt;{Data_Load}, 4, 2);
		$Refined-&gt;{Ref} = _bytes_to_int($event-&gt;{Data_Load}, 6, 4);

		if (exists $Me-&gt;{_Sent_Requests}{$Refined-&gt;{Ref}}) {
			$Refined-&gt;{Responce_Type} = $Me-&gt;{_Sent_Requests}{$Refined-&gt;{Ref}};
			undef $Me-&gt;{_Sent_Requests}{$Refined-&gt;{Ref}};
		}

		#first ten is SNAC header, then a 00 01 (normally..) then the message's size in
		#Normal then endian format (don't have any idea why, but it is..) but skip all that..
		$i = 16;
		$Refined-&gt;{Our_UIN} = _endian_bytes_to_int($event-&gt;{Data_Load}, $i, 4);$i += 4;

		#the first of the sub sub types..
		$Refined-&gt;{MessageType} = _endian_bytes_to_int($event-&gt;{Data_Load}, $i, 2);$i += 2;
		# print "\n&gt;&gt; ",$Refined-&gt;{MessageType}," &lt;&lt; \n";
		if ($Refined-&gt;{MessageType} == 65){
			# normally offline messages..
			if (_endian_bytes_to_int($event-&gt;{Data_Load}, $i, 2) == 2) {
				#90% sure it's an offline message..
				$i += 2;
				$Refined-&gt;{Sender} = _endian_bytes_to_int($event-&gt;{Data_Load}, $i, 4);$i += 4;

				#note, the time given is in GMT, not local, so make it local..(DIE AOL!!!)
				$Refined-&gt;{Sent_Time} = localtime(timegm(0,
					_endian_bytes_to_int($event-&gt;{Data_Load}, $i+5, 1),
					_endian_bytes_to_int($event-&gt;{Data_Load}, $i+4, 1),
					_endian_bytes_to_int($event-&gt;{Data_Load}, $i+3, 1),
					_endian_bytes_to_int($event-&gt;{Data_Load}, $i+2, 1)-1,
					_endian_bytes_to_int($event-&gt;{Data_Load}, $i,2)));
				$i += 6;

				$Refined-&gt;{Message_Encoding} = _endian_bytes_to_int($event-&gt;{Data_Load}, $i, 1);
				$i++;
				$Refined-&gt;{Message_Flags} = _endian_bytes_to_int($event-&gt;{Data_Load}, $i, 1);
				$i+=1;
				my $DataLength=0;
				# print "\n $DataLength \n//==&gt; $Refined-&gt;{Message_Encoding} $Refined-&gt;{Message_Flags}\n\n";
				if ($Refined-&gt;{Message_Encoding} == 1) {
					#normal text message..
					$Refined-&gt;{MessageType} = "offline_text_message";
					$DataLength = _bytes_to_int ($event-&gt;{Data_Load}, $i, 2);
					$Refined-&gt;{text} = _bytes_to_str($event-&gt;{Data_Load}, $i+2, $DataLength);
					$Refined-&gt;{text} =~ s/\r\n/\n/g;
				} elsif ($Refined-&gt;{Message_Encoding} == 4) {
					$Refined-&gt;{MessageType} = "URL";
					# $DataLength = _bytes_to_int ($event-&gt;{Data_Load}, $i, 2);
					# my $data = _bytes_to_str($event-&gt;{Data_Load}, $i+2, $DataLength);
					# ($Refined-&gt;{Description},$Refined-&gt;{URL}) = split /\xfe/,$data;
					($Refined-&gt;{Description},$Refined-&gt;{URL}) = GetURLMessage($event-&gt;{Data_Load}, $i);
				} elsif ($Refined-&gt;{Message_Encoding} == 5) {
					my $DataLength = _bytes_to_int($event-&gt;{Data_Load}, $i, 2);
					$i+=2;
					$i+=4;
					my $type = ord(_bytes_to_str($event-&gt;{Data_Load}, $i, 1));
					$i++;
					# print "++&gt; $type";
					if($type == 48) # Auth request!
					{
						$i++;
						my $data = _bytes_to_str($event-&gt;{Data_Load}, $i, $DataLength-4);
						my($nick,$fname,$lname,$email,$xxx,$reason);
						$Refined-&gt;{nick} = $nick;
						$Refined-&gt;{first_name} = $fname;
						$Refined-&gt;{last_name} = $lname;
						$Refined-&gt;{email} = $email;
						$Refined-&gt;{reason} = $data;
						$Refined-&gt;{MessageType} = "auth_request";
					}

				} elsif ($Refined-&gt;{Message_Encoding} == 26) {
					$i += 2;
					my $type = _bytes_to_int($event-&gt;{Data_Load}, $i, 2);
					if($type == 0x39) {
						$i += 21;
						my $length = _endian_bytes_to_int($event-&gt;{Data_Load}, $i, 4);
						$i += 4;
						my $s = _bytes_to_str($event-&gt;{Data_Load},$i,$length);
						$i += $length;
						$i += 19;
						$length = _endian_bytes_to_int($event-&gt;{Data_Load}, $i, 4);
						$i += 4;
						my $reason = _bytes_to_str($event-&gt;{Data_Load},$i,$length);
						$Refined-&gt;{MessageType} = "contacts_request";
						$Refined-&gt;{Reason} = $reason;

					} elsif($type == 0x20) {
						$i += 21;
						my $length = _endian_bytes_to_int($event-&gt;{Data_Load}, $i, 4);
						$i += 4;
						my $s = _bytes_to_str($event-&gt;{Data_Load},$i,$length);
						$i += $length;
						$i += 7;
						$length = _endian_bytes_to_int($event-&gt;{Data_Load}, $i, 4);
						$i += 4;
						$s = _bytes_to_str($event-&gt;{Data_Load},$i,$length);
						$Refined = _Decode_Tagged_Text($s);
					}

				} elsif ($Refined-&gt;{Message_Encoding} == 14) {

					$DataLength = _bytes_to_int ($event-&gt;{Data_Load}, $i, 2);
					my $data = _bytes_to_str($event-&gt;{Data_Load}, $i+2, $DataLength);
					$Refined-&gt;{MessageType} = 'email_message';
					my ($name,$junk,$junk2,$email,$junk3,$text) = split /\xfe/,$data;
					$Refined-&gt;{Name} = $name;
					$Refined-&gt;{Email} = $email;
					$Refined-&gt;{Text} = $text;
				} elsif ($Refined-&gt;{Message_Encoding} == 19) {
						$DataLength = _bytes_to_int ($event-&gt;{Data_Load}, $i, 2);
						$i+=2;
						my $data = _bytes_to_str($event-&gt;{Data_Load}, $i, $DataLength-4);
						my ($contactcount,@contacts) = split /\xfe/,$data;
						$Refined-&gt;{MessageType} = "contacts";
						$Refined-&gt;{Count} = $contactcount;
						$Refined-&gt;{Contacts} = \@contacts;
				} elsif ($Refined-&gt;{Message_Encoding} == 6) {
						$DataLength = _bytes_to_int ($event-&gt;{Data_Load}, $i, 2);
						$i+=2;
						my $data = _bytes_to_str($event-&gt;{Data_Load}, $i, $DataLength-4);
						my($nick,$fname,$lname,$email,$xxx,$reason) = split /\xfe/,$data;;
						$Refined-&gt;{nick} = $nick;
						$Refined-&gt;{first_name} = $fname;
						$Refined-&gt;{last_name} = $lname;
						$Refined-&gt;{email} = $email;
						$Refined-&gt;{reason} = $reason;
						$Refined-&gt;{MessageType} = "auth_request";

				}

			} else {
				print "Argh, something Screwed up!!!";
				return;
			}

		} elsif ($Refined-&gt;{MessageType} == 66) {
			# End of offline messages
			$Me-&gt;Send_Command("Cmd_Srv_Message", {MessageType =&gt; "ack_offline"});
			$Refined-&gt;{MessageType} = "ack_offline";
		} elsif ($Refined-&gt;{MessageType} == 2010) {
			#Server messages stored in "html" style tags..
			$i += 2;


			$Refined-&gt;{SubMessageType} = _bytes_to_int($event-&gt;{Data_Load}, $i, 3); $i+=3;
			# print "&gt;&gt; ", $Refined-&gt;{SubMessageType}, "\n";

			if($Refined-&gt;{SubMessageType} == 9830410) {
				if (_bytes_to_int($event-&gt;{Data_Load}, $i, 2) == 41480) {
					#short gap.. (this is a VERY bad way of doing this.. should fix..)
					$i += 3;
				} else {
					#don't know what these 11(?) bytes do..
					$i += 11;
				}


				$Refined-&gt;{TaggedDataString} = _bytes_to_str($event-&gt;{Data_Load}, $i+3, _bytes_to_int($event-&gt;{Data_Load}, $i, 2));
				$Refined = _Decode_Tagged_Text($Refined-&gt;{TaggedDataString}, $Refined);

			} elsif($Refined-&gt;{SubMessageType} == 262410) {
				my($BytesToCount);
				$BytesToCount		= _bytes_to_int($event-&gt;{Data_Load}, $i, 1); $i+=2;
				$Refined-&gt;{Nickname}	= _bytes_to_str($event-&gt;{Data_Load}, $i, $BytesToCount-1); $i += $BytesToCount;
				$BytesToCount		= _bytes_to_int($event-&gt;{Data_Load}, $i, 1); $i+=2;
				$Refined-&gt;{Firstname}	= _bytes_to_str($event-&gt;{Data_Load}, $i, $BytesToCount-1); $i += $BytesToCount;
				$BytesToCount		= _bytes_to_int($event-&gt;{Data_Load}, $i, 1); $i+=2;
				$Refined-&gt;{Lastname}	= _bytes_to_str($event-&gt;{Data_Load}, $i, $BytesToCount-1); $i += $BytesToCount;
				$BytesToCount		= _bytes_to_int($event-&gt;{Data_Load}, $i, 1); $i+=2;
				$Refined-&gt;{Email}	= _bytes_to_str($event-&gt;{Data_Load}, $i, $BytesToCount-1); $i += $BytesToCount;
				$BytesToCount		= _bytes_to_int($event-&gt;{Data_Load}, $i, 1); $i+=2;
				$Refined-&gt;{MessageType}	= 'user_short_info';

			} elsif($Refined-&gt;{SubMessageType} == 262420) {
				$Refined-&gt;{MessageType} = "user_info_not_found";
			} elsif($Refined-&gt;{SubMessageType} == 10618890) {
				#Ads stuff
				$Refined-&gt;{MessageType} = "Tagged_Srv_Responce";
				if (_bytes_to_int($event-&gt;{Data_Load}, $i, 2) == 41480) {
					#short gap.. (this is a VERY bad way of doing this.. should fix..)
					$i += 3;
				} else {
					#don't know what these 11(?) bytes do..
					$i += 11;
				}

				$Refined-&gt;{TaggedDataString} = _bytes_to_str($event-&gt;{Data_Load}, $i+2, _bytes_to_int($event-&gt;{Data_Load}, $i, 2));
				$Refined = _Decode_Tagged_Text($Refined-&gt;{TaggedDataString}, $Refined);

			} elsif($Refined-&gt;{SubMessageType} == 10748170) {
			#Info Request return)
				my($BytesToCount);
				$Refined-&gt;{MessageType} ="wp_result_info";
				#Unknown word
				$i += 2;
				$Refined-&gt;{UIN}		= _endian_bytes_to_int($event-&gt;{Data_Load}, $i, 4); $i += 4;
				$BytesToCount		= _bytes_to_int($event-&gt;{Data_Load}, $i, 1); $i+=2;
				$Refined-&gt;{Nickname}	= _bytes_to_str($event-&gt;{Data_Load}, $i, $BytesToCount-1); $i += $BytesToCount;
				$BytesToCount		= _bytes_to_int($event-&gt;{Data_Load}, $i, 1); $i+=2;
				$Refined-&gt;{Firstname}	= _bytes_to_str($event-&gt;{Data_Load}, $i, $BytesToCount-1); $i += $BytesToCount;
				$BytesToCount		= _bytes_to_int($event-&gt;{Data_Load}, $i, 1); $i+=2;
				$Refined-&gt;{Lastname}	= _bytes_to_str($event-&gt;{Data_Load}, $i, $BytesToCount-1); $i += $BytesToCount;
				$BytesToCount		= _bytes_to_int($event-&gt;{Data_Load}, $i, 1); $i+=2;
				$Refined-&gt;{Email}	= _bytes_to_str($event-&gt;{Data_Load}, $i, $BytesToCount-1); $i += $BytesToCount;
				$Refined-&gt;{Auth_Required} = _bytes_to_int($event-&gt;{Data_Load}, $i, 1); $i +=1;
				$Refined-&gt;{Status}	= _bytes_to_int($event-&gt;{Data_Load}, $i, 1); $i += 1;
				#always ends with a 00

			} elsif($Refined-&gt;{SubMessageType} == 10748210 || $Refined-&gt;{SubMessageType} == 11403570) {
				$Refined-&gt;{MessageType} = "wp_empty";
				#Empty White Page Result

			} elsif(($Refined-&gt;{SubMessageType} &amp; 0xffff00) == 0x640000){ #11403530) {
				$Refined-&gt;{MessageType} ="set_main_info_ack";
			} elsif(($Refined-&gt;{SubMessageType} &amp; 0xffff00) == 0xA00000){ #11403530) {
				$Refined-&gt;{MessageType} ="set_permissions_ack";
			} elsif(($Refined-&gt;{SubMessageType} &amp; 0xffff00) == 0xae0100){ #11403530) {
				my($BytesToCount);
				$Refined-&gt;{MessageType} ="wp_final_result_info";
				#Unknown word
				$i += 2;
				$Refined-&gt;{UIN}		= _endian_bytes_to_int($event-&gt;{Data_Load}, $i, 4); $i += 4;
				$BytesToCount		= _bytes_to_int($event-&gt;{Data_Load}, $i, 1); $i+=2;
				$Refined-&gt;{Nickname}	= _bytes_to_str($event-&gt;{Data_Load}, $i, $BytesToCount-1); $i += $BytesToCount;
				$BytesToCount		= _bytes_to_int($event-&gt;{Data_Load}, $i, 1); $i+=2;
				$Refined-&gt;{Firstname}	= _bytes_to_str($event-&gt;{Data_Load}, $i, $BytesToCount-1); $i += $BytesToCount;
				$BytesToCount		= _bytes_to_int($event-&gt;{Data_Load}, $i, 1); $i+=2;
				$Refined-&gt;{Lastname}	= _bytes_to_str($event-&gt;{Data_Load}, $i, $BytesToCount-1); $i += $BytesToCount;
				$BytesToCount		= _bytes_to_int($event-&gt;{Data_Load}, $i, 1); $i+=2;
				$Refined-&gt;{Email}	= _bytes_to_str($event-&gt;{Data_Load}, $i, $BytesToCount-1); $i += $BytesToCount;
				$Refined-&gt;{Auth_Required} = _bytes_to_int($event-&gt;{Data_Load}, $i, 1); $i +=1;
				$Refined-&gt;{Status}	= _bytes_to_int($event-&gt;{Data_Load}, $i, 1); $i += 1;
				#Some weird 3 bytes are thrown in - perhaps
				#a counter for total unreturned results?
				#always ends with 00

			} elsif($Refined-&gt;{SubMessageType} == 13107210) {
				my ($BytesToCount);
				$Refined-&gt;{MessageType} = "user_info_main";

				#This isn't really correct, since it's endian data and not normal, but
				# this will only be shown if any name etc is longer then 255 chars..
				$BytesToCount		= _bytes_to_int($event-&gt;{Data_Load}, $i, 1); $i+=2;
				$Refined-&gt;{Nickname}	= _bytes_to_str($event-&gt;{Data_Load}, $i, $BytesToCount-1); $i += $BytesToCount;
				$BytesToCount		= _bytes_to_int($event-&gt;{Data_Load}, $i, 1); $i+=2;
				$Refined-&gt;{Firstname}	= _bytes_to_str($event-&gt;{Data_Load}, $i, $BytesToCount-1); $i += $BytesToCount;
				$BytesToCount		= _bytes_to_int($event-&gt;{Data_Load}, $i, 1); $i+=2;
				$Refined-&gt;{Lastname}	= _bytes_to_str($event-&gt;{Data_Load}, $i, $BytesToCount-1); $i += $BytesToCount;
				$BytesToCount		= _bytes_to_int($event-&gt;{Data_Load}, $i, 1); $i+=2;
				$Refined-&gt;{Email}	= _bytes_to_str($event-&gt;{Data_Load}, $i, $BytesToCount-1); $i += $BytesToCount;
				$BytesToCount		= _bytes_to_int($event-&gt;{Data_Load}, $i, 1); $i+=2;
				$Refined-&gt;{City}	= _bytes_to_str($event-&gt;{Data_Load}, $i, $BytesToCount-1); $i += $BytesToCount;
				$BytesToCount		= _bytes_to_int($event-&gt;{Data_Load}, $i, 1); $i+=2;
				$Refined-&gt;{State}	= _bytes_to_str($event-&gt;{Data_Load}, $i, $BytesToCount-1); $i += $BytesToCount;
				$BytesToCount		= _bytes_to_int($event-&gt;{Data_Load}, $i, 1); $i+=2;
				$Refined-&gt;{Telephone}	= _bytes_to_str($event-&gt;{Data_Load}, $i, $BytesToCount-1); $i += $BytesToCount;
				$BytesToCount		= _bytes_to_int($event-&gt;{Data_Load}, $i, 1); $i+=2;
				$Refined-&gt;{Fax_Num}	= _bytes_to_str($event-&gt;{Data_Load}, $i, $BytesToCount-1); $i += $BytesToCount;
				$BytesToCount		= _bytes_to_int($event-&gt;{Data_Load}, $i, 1); $i+=2;
				$Refined-&gt;{Address}	= _bytes_to_str($event-&gt;{Data_Load}, $i, $BytesToCount-1); $i += $BytesToCount;
				$BytesToCount		= _bytes_to_int($event-&gt;{Data_Load}, $i, 1); $i+=2;
				$Refined-&gt;{Mobile_Phone} = _bytes_to_str($event-&gt;{Data_Load}, $i, $BytesToCount-1); $i += $BytesToCount;
				$BytesToCount		= _bytes_to_int($event-&gt;{Data_Load}, $i, 1); $i+=2;
				$Refined-&gt;{Zip}		= _bytes_to_str($event-&gt;{Data_Load}, $i, $BytesToCount-1); $i += $BytesToCount;
				$Refined-&gt;{Country}	= _bytes_to_int($event-&gt;{Data_Load}, $i, 1); $i += 2;
				$Refined-&gt;{GMT_Code}	= _bytes_to_int($event-&gt;{Data_Load}, $i, 1); $i += 2;

			} elsif($Refined-&gt;{SubMessageType} == 15400970) {
				my($BytesToCount, $Extra_Email_Count);
				$Refined-&gt;{MessageType} = "user_info_extra_emails";

				$Extra_Email_Count = $Refined-&gt;{Extra_Email_Count} = _bytes_to_int($event-&gt;{Data_Load}, $i, 1); $i += 2;

				#Grab all the extra E-mails, and place them into an array..
				while ($Extra_Email_Count &gt; 0) {
					$BytesToCount = _bytes_to_int($event-&gt;{Data_Load}, $i, 1); $i+=2;
					push(@{$Refined-&gt;{Extra_Emails}}, _bytes_to_str($event-&gt;{Data_Load}, $i, $BytesToCount-1)); $i += $BytesToCount+1;
					$Extra_Email_Count--;
				}

			} elsif($Refined-&gt;{SubMessageType} == 14417930) {
				my ($BytesToCount);
				$Refined-&gt;{MessageType} = "user_info_homepage";

				#one of the 0 bytes may be the homepage category, but who cares about that
				$Refined-&gt;{Age}		= _bytes_to_int($event-&gt;{Data_Load}, $i, 1); $i += 2;
				$Refined-&gt;{Sex}		= _bytes_to_int($event-&gt;{Data_Load}, $i, 1); $i += 1;
				$BytesToCount		= _bytes_to_int($event-&gt;{Data_Load}, $i, 1); $i+=2;
				$Refined-&gt;{Homepage}	= _bytes_to_str($event-&gt;{Data_Load}, $i, $BytesToCount-1); $i += $BytesToCount;
				$Refined-&gt;{Birth_Year}	= _endian_bytes_to_int($event-&gt;{Data_Load}, $i, 2); $i +=2;
				$Refined-&gt;{Birth_Month}	= _bytes_to_int($event-&gt;{Data_Load}, $i, 1); $i +=1;
				$Refined-&gt;{Birth_Day}	= _bytes_to_int($event-&gt;{Data_Load}, $i, 1); $i +=1;
				$Refined-&gt;{Language1}	= _bytes_to_int($event-&gt;{Data_Load}, $i, 1); $i +=1;
				$Refined-&gt;{Language2}	= _bytes_to_int($event-&gt;{Data_Load}, $i, 1); $i +=1;
				$Refined-&gt;{Language3}	= _bytes_to_int($event-&gt;{Data_Load}, $i, 1); $i +=1;

			} elsif($Refined-&gt;{SubMessageType} == 917770) {
				$Refined-&gt;{MessageType} = "user_info_unknown";
			} elsif($Refined-&gt;{SubMessageType} == 16384020) {
				$Refined-&gt;{MessageType} = "user_info_not_found";
			} elsif($Refined-&gt;{SubMessageType} == 13762570) {
				my($BytesToCount);
				$Refined-&gt;{MessageType} = "user_info_work";

				#work DC000A
				$BytesToCount			= _bytes_to_int($event-&gt;{Data_Load}, $i, 1); $i+=2;
				$Refined-&gt;{Company_City}	= _bytes_to_str($event-&gt;{Data_Load}, $i, $BytesToCount-1); $i += $BytesToCount;
				$BytesToCount			= _bytes_to_int($event-&gt;{Data_Load}, $i, 1); $i+=2;
				$Refined-&gt;{Company_State}	= _bytes_to_str($event-&gt;{Data_Load}, $i, $BytesToCount-1); $i += $BytesToCount;
				$BytesToCount			= _bytes_to_int($event-&gt;{Data_Load}, $i, 1); $i+=2;
				$Refined-&gt;{Company_Phone}	= _bytes_to_str($event-&gt;{Data_Load}, $i, $BytesToCount-1); $i += $BytesToCount;
				$BytesToCount			= _bytes_to_int($event-&gt;{Data_Load}, $i, 1); $i+=2;
				$Refined-&gt;{Company_Fax}		= _bytes_to_str($event-&gt;{Data_Load}, $i, $BytesToCount-1); $i += $BytesToCount;

				# odd 6 bytes, 2 sets of 01 00 00, almost
				# like 2 sets of dwords that are empty
				# $i += 4;
				$BytesToCount			= _bytes_to_int($event-&gt;{Data_Load}, $i, 1); $i+=2;
				$Refined-&gt;{Company_Address}	= _bytes_to_str($event-&gt;{Data_Load}, $i, $BytesToCount-1); $i += $BytesToCount;
				$BytesToCount			= _bytes_to_int($event-&gt;{Data_Load}, $i, 1); $i+=2;
				$Refined-&gt;{Company_Zip}		= _bytes_to_str($event-&gt;{Data_Load}, $i, $BytesToCount-1); $i += $BytesToCount;
				# $i+=2;
				$Refined-&gt;{Company_Country}	= _bytes_to_int($event-&gt;{Data_Load}, $i, 1); $i +=2;
				$BytesToCount			= _bytes_to_int($event-&gt;{Data_Load}, $i, 1); $i+=2;
				$Refined-&gt;{Company_Name}	= _bytes_to_str($event-&gt;{Data_Load}, $i, $BytesToCount-1); $i += $BytesToCount;
				$BytesToCount			= _bytes_to_int($event-&gt;{Data_Load}, $i, 1); $i+=2;
				$Refined-&gt;{Company_Department}	= _bytes_to_str($event-&gt;{Data_Load}, $i, $BytesToCount-1); $i += $BytesToCount;
				$BytesToCount			= _bytes_to_int($event-&gt;{Data_Load}, $i, 1); $i+=2;
				$Refined-&gt;{Company_Position}	= _bytes_to_str($event-&gt;{Data_Load}, $i, $BytesToCount-1); $i += $BytesToCount;
				$Refined-&gt;{Company_Occupation}	= _bytes_to_int($event-&gt;{Data_Load}, $i, 1); $i += 2;
				$BytesToCount			= _bytes_to_int($event-&gt;{Data_Load}, $i, 1); $i+=2;
				$Refined-&gt;{Company_URL}		= _bytes_to_str($event-&gt;{Data_Load}, $i, $BytesToCount-1); $i += $BytesToCount;

			} elsif($Refined-&gt;{SubMessageType} == 15073290) {
			#about)
				my ($BytesToCount);
				$Refined-&gt;{MessageType} = "user_info_about";
				$BytesToCount = _bytes_to_int($event-&gt;{Data_Load}, $i, 1); $i+=2;
				$Refined-&gt;{about} = _bytes_to_str($event-&gt;{Data_Load}, $i, $BytesToCount-1); $i += $BytesToCount;
			} elsif($Refined-&gt;{SubMessageType} == 15728650 ) {
			#Personal Interests)
				my ($BytesToCount, $Int_Count);
				$Refined-&gt;{MessageType} = "user_info_personal_interests";

				$Int_Count = $Refined-&gt;{Interests_Count} = _bytes_to_int($event-&gt;{Data_Load}, $i, 1); $i+=1;

				while ($Int_Count &gt;0) {
					$Int_Count--;

					push(@{$Refined-&gt;{Interests_Type}}, _bytes_to_int($event-&gt;{Data_Load}, $i, 2)); $i += 2;
					$BytesToCount = _bytes_to_int($event-&gt;{Data_Load}, $i, 1); $i+=2;
					push(@{$Refined-&gt;{Interests_Desc}}, _bytes_to_str($event-&gt;{Data_Load}, $i, $BytesToCount-1)); $i += $BytesToCount;
				}

			} elsif($Refined-&gt;{SubMessageType} == 16384010) {
			#Past Interests Info)
				$Refined-&gt;{MessageType} = "user_info_past_background";
				$Refined-&gt;{_background_count} = _bytes_to_int($event-&gt;{Data_Load}, $i, 1); $i+=1;
				if($Refined-&gt;{_background_count} &gt; 0) {
					$Refined-&gt;{_background_category1} = _bytes_to_int($event-&gt;{Data_Load}, $i, 2); $i += 2;
					$Refined-&gt;{BytesToCount} = _bytes_to_int($event-&gt;{Data_Load}, $i, 1); $i+=2;
					$Refined-&gt;{_background_description1} = _bytes_to_str($event-&gt;{Data_Load}, $i, $Refined-&gt;{BytesToCount}-1); $i += $Refined-&gt;{BytesToCount};
				}
				if($Refined-&gt;{_background_count} &gt; 1) {
					$Refined-&gt;{_background_category2} = _bytes_to_int($event-&gt;{Data_Load}, $i, 2); $i += 2;
					$Refined-&gt;{BytesToCount} = _bytes_to_int($event-&gt;{Data_Load}, $i, 1); $i+=2;
					$Refined-&gt;{_background_description2} = _bytes_to_str($event-&gt;{Data_Load}, $i, $Refined-&gt;{BytesToCount}-1); $i += $Refined-&gt;{BytesToCount};
				}
				if($Refined-&gt;{_background_count} &gt; 2) {
					$Refined-&gt;{_background_category3} = _bytes_to_int($event-&gt;{Data_Load}, $i, 2); $i += 2;
					$Refined-&gt;{BytesToCount} = _bytes_to_int($event-&gt;{Data_Load}, $i, 1); $i+=2;
					$Refined-&gt;{_background_description3} = _bytes_to_str($event-&gt;{Data_Load}, $i, $Refined-&gt;{BytesToCount}-1); $i += $Refined-&gt;{BytesToCount};
				}
				$Refined-&gt;{_organization_count} = _bytes_to_int($event-&gt;{Data_Load}, $i, 1); $i+=1;
				if($Refined-&gt;{_organization_count} &gt; 0) {
					$Refined-&gt;{_organization_category1} = _bytes_to_int($event-&gt;{Data_Load}, $i, 2); $i += 2;
					$Refined-&gt;{BytesToCount} = _bytes_to_int($event-&gt;{Data_Load}, $i, 1); $i+=2;
					$Refined-&gt;{_organization_description1} = _bytes_to_str($event-&gt;{Data_Load}, $i, $Refined-&gt;{BytesToCount}-1); $i += $Refined-&gt;{BytesToCount};
				}
				if($Refined-&gt;{_organization_count} &gt; 1) {
					$Refined-&gt;{_organization_category2} = _bytes_to_int($event-&gt;{Data_Load}, $i, 2); $i += 2;
					$Refined-&gt;{BytesToCount} = _bytes_to_int($event-&gt;{Data_Load}, $i, 1); $i+=2;
					$Refined-&gt;{_organization_description2} = _bytes_to_str($event-&gt;{Data_Load}, $i, $Refined-&gt;{BytesToCount}-1); $i += $Refined-&gt;{BytesToCount};
				}
				if($Refined-&gt;{_organization_count} &gt; 2) {
					$Refined-&gt;{_organization_category3} = _bytes_to_int($event-&gt;{Data_Load}, $i, 2); $i += 2;
					$Refined-&gt;{BytesToCount} = _bytes_to_int($event-&gt;{Data_Load}, $i, 1); $i+=2;
					$Refined-&gt;{_organization_description3} = _bytes_to_str($event-&gt;{Data_Load}, $i, $Refined-&gt;{BytesToCount}-1); $i += $Refined-&gt;{BytesToCount};
				}
			} elsif($Refined-&gt;{SubMessageType} == 6685450) {
				$Refined-&gt;{MessageType} = "random_uin";
				$Refined-&gt;{UIN} = _endian_bytes_to_int($event-&gt;{Data_Load}, $i, 4); $i+=4;
				$Refined-&gt;{Sender} = "010"; # hmmmm
			} elsif($Refined-&gt;{SubMessageType} == 0x70030a) {
				$Refined-&gt;{MessageType} = "group_set";
				$Refined-&gt;{Sender} = "010"; # hmmmm
			} elsif($Refined-&gt;{SubMessageType} == 0xaa000a) {
				$Refined-&gt;{MessageType} = "password_set";
				$Refined-&gt;{Sender} = "010"; # hmmmm
			} elsif($Refined-&gt;{SubMessageType} == 0x00010046) {
				$Refined-&gt;{MessageType} = "sms_error";
				my $length = 0;
				# NTS with error message following
				while($event-&gt;{Data_Load}[$i+$length] != 0) {
					$length++;
				}
				$Refined-&gt;{Text} = _bytes_to_str($event-&gt;{Data_Load}, $i, $length);
			} else {
				print "&gt;&gt;Unknown SubMessageType: ", $Refined-&gt;{SubMessageType}, "\n";
			}

		}

		return ($Refined);
	}
);

%_Cmd_Codes = (
	Cmd_GSC_Client_Ready =&gt;		'1:2',
	Cmd_GSC_Reqest_Rate_Info =&gt;	'1:6',
	Cmd_GSC_LoggedIn_User_Info =&gt;	'1:14',
	Cmd_GSC_ICQInform =&gt;		'1:23',
	Cmd_GSC_Set_Status =&gt;		'1:30',
	Cmd_LS_LoggedIn_User_Rights =&gt;	'2:2',
	Cmd_LS_Set_User_Info =&gt;		'2:4',
	Cmd_BLM_Rights_Info =&gt;		'3:2',
	Cmd_CTL_UploadList =&gt;		'3:4',
	Cmd_Mes_Add_ICBM_Param =&gt;	'4:2',
	Cmd_Mes_Param_Info =&gt;		'4:4',
	Cmd_Send_Message =&gt;		'4:6',
	Cmd_Type2_Ack	=&gt;		'4:11',

	Cmd_BOS_Get_Rights =&gt;		'9:2',
	Cmd_BOS_Add_VisibleList =&gt;	'9:5',
	Cmd_BOS_Remove_VisibleList =&gt;	'9:6',
	Cmd_BOS_Add_InVisibleList =&gt;	'9:7',
	Cmd_BOS_Remove_InVisibleList =&gt;	'9:8',
	Cmd_BOS_Add_GenericList =&gt;	'9:10',
	Cmd_BOS_Remove_GenericList =&gt;	'9:11',

	Cmd_Init_Contacts =&gt;		'19:2',
	Cmd_Get_List =&gt;			'19:5',
	Cmd_Add_List =&gt;			'19:8',
	Cmd_Update_List =&gt;		'19:9',
# 	Cmd_RemoveVisibleList =&gt;	'19:10',
	Cmd_Del_List =&gt;			'19:10',
	Cmd_Start_ListUpdate =&gt; 	'19:17',
	Cmd_Stop_ListUpdate =&gt;		'19:18',
	Cmd_Add_ContactList =&gt;		'19:20',
	Cmd_Req_Auth =&gt; 		'19:24',
	Cmd_Authorize =&gt;		'19:26',

	Cmd_Srv_Message =&gt;		'21:2',

	Cmd_Register =&gt;			'23:4'
);

%_Srv_Codes = (
	Srv_GSC_Error =&gt;		'1:1',
	Srv_GSC_Ready =&gt;		'1:3',
	Srv_GSC_Redirect =&gt;		'1:5',
	Srv_GSC_Rate_Info =&gt;		'1:7',
	Srv_GSC_Rate_Change =&gt;		'1:10',
	Srv_GSC_User_Info =&gt;		'1:15',
	Srv_GSC_MOTD =&gt;			'1:19',
	Srv_GSC_ICQClientConfirm =&gt;	'1:24',
	Srv_LS_Rights_Response =&gt;	'2:3',
	Srv_BLM_Rights_Response =&gt;	'3:3',
	Srv_BLM_Contact_Online =&gt;	'3:11',
	Srv_BLM_Contact_Offline =&gt;	'3:12',
	Srv_Mes_Rights_Response =&gt;	'4:5',
	Srv_Mes_Received =&gt;		'4:7',
	Srv_Usr_Typing	 =&gt;		'4:20',
	Srv_BOS_Rights =&gt;		'9:3',
	Srv_Password_Missmatch =&gt;	'9:9',
	Srv_Srv_Message =&gt;		'21:3',
	Srv_Contact_List =&gt;		'19:6',
	Srv_List_Ack   =&gt;		'19:14',
	Srv_Auth_Ack   =&gt;		'19:27',
	Srv_You_Were_Added =&gt;    	'19:28',
	Srv_Registered =&gt;		'23:7',
	DC_Message =&gt;			'DC:2',
	File_Offer =&gt;			'File:2'
);


%_New_Connection_Nefotiation_Codes = (
	1 =&gt; sub {
		my ($Me, $event) = @_;
		my($Responce);
		if($Me-&gt;{_Register}) {
			$Responce-&gt;{Channel_ID} = 1;
			@{$Responce-&gt;{Data_Load}} = _int_to_bytes(4, 1);
			push(@{$Me-&gt;{_Outgoing_Queue}}, $Responce);
			$Me-&gt;Send_Command("Cmd_Register",{ password =&gt; $Me-&gt;{_Password}});
			return;
		}
		# print "Sending Connection reply..\n";
		print ".";

		if ($Me-&gt;{_Connection_Cookie}) {
			# print "Sending Cookie\n";
			print ".";
			#Second time connected, so send the cookie..
			$Responce-&gt;{Channel_ID} = 1;
			@{$Responce-&gt;{Data_Load}} = _int_to_bytes(4, 1);
			push(@{$Responce-&gt;{Data_Load}}, _Write_TLV(1, 'Connection_Cookie', $Me-&gt;{_Connection_Cookie}));

			push(@{$Me-&gt;{_Outgoing_Queue}}, $Responce);

			#wipe the now used cookie (eat? :)
			$Me-&gt;{_Connection_Cookie} = 0;
			return;
		}

		#send our login details..
		$Responce-&gt;{Channel_ID} = 1;
		@{$Responce-&gt;{Data_Load}} = _int_to_bytes(2, 0);
		push(@{$Responce-&gt;{Data_Load}}, _int_to_bytes(2, 1));
		push(@{$Responce-&gt;{Data_Load}}, _Write_TLV(1, 'UIN', $Me-&gt;{_UIN}));
		push(@{$Responce-&gt;{Data_Load}}, _Write_TLV(1, 'Password', &amp;_Password_Encrypt($Me-&gt;{_Password})));
		push(@{$Responce-&gt;{Data_Load}}, _Write_TLV(1, 'ClientProfile', "ICQ Inc. - Product of ICQ (TM).2000b.4.63.1.3279.85"));
		push(@{$Responce-&gt;{Data_Load}}, _Write_TLV(1, 'ClientType', 266));
		push(@{$Responce-&gt;{Data_Load}}, _Write_TLV(1, 'ClientVersionMajor', 5));
		push(@{$Responce-&gt;{Data_Load}}, _Write_TLV(1, 'ClientVersionMinor', 63));
		push(@{$Responce-&gt;{Data_Load}}, _Write_TLV(1, 'ClientICQNumber', 1));
		push(@{$Responce-&gt;{Data_Load}}, _Write_TLV(1, 'ClientBuildMajor', 3279));
		push(@{$Responce-&gt;{Data_Load}}, _Write_TLV(1, 'ClientBuildMinor', 85));
		push(@{$Responce-&gt;{Data_Load}}, _Write_TLV(1, 'Language', "en"));
		push(@{$Responce-&gt;{Data_Load}}, _Write_TLV(1, 'CountryCode', "us"));
		push(@{$Me-&gt;{_Outgoing_Queue}}, $Responce);
	}
);

sub _Disconnection_Nefotiation
{
	my ($Me, $event) = @_;
	my ($Details, $i);
	print "xIncoming..\n" if $Me-&gt;{_Debug};
	_print_packet($event-&gt;{Data_Load}, ()) if $Me-&gt;{_Debug};

	($Details, $i) = &amp;_Read_TLV($event-&gt;{Data_Load}, 4);
	if($Details-&gt;{Error_Code}) {

		$Me-&gt;SetErrorCode($Details-&gt;{Error_Code});
		$Me-&gt;Disconnect;
		return -1;
	}

	if($Details-&gt;{Dual_User_Online}) {
		$Me-&gt;SetError("Another user logged in with your UIN, disconnected..");
		$Me-&gt;Disconnect;
		return -1;
	}
	# if ($Details-&gt;{UIN} != $Me-&gt;{_UIN})
	# {
		# $Me-&gt;SetError("Server got our UIN wrong!![".$Details-&gt;{UIN}."]");
		# return;
	# }

	$Me-&gt;{_Connection_Cookie} = $Details-&gt;{Connection_Cookie} if ($Details-&gt;{Connection_Cookie});
	if ($Details-&gt;{Server_And_Port}) {
		#we've been told to disconnect, and reconnect...
		# print "Disconnecting as instructed..\n";
		$Me-&gt;Disconnect();

		#change the server we are going to access...
		($Me-&gt;{_Server}, $Me-&gt;{_ServerPort}) = split (/:/, $Details-&gt;{Server_And_Port});
		print "Changing to server [".$Me-&gt;{_Server}."][".$Me-&gt;{_ServerPort}."]\n" if ($Me-&gt;{_Debug});

		$Me-&gt;Connect();
	} elsif ($Details-&gt;{Password_Error}) {
		#run the PasswordError hook 9,9
		# if (exists $Me-&gt;{_Hooks}{$_-&gt;{9}{9}} ) {
			# &amp;{$Me-&gt;{_Hooks}{9}{9}}($Me, $_)
		# }

		if (exists $Me-&gt;{_Hooks}{"9:9"} ) {
			&amp;{$Me-&gt;{_Hooks}{"9:9"}}($Me, $_)
		}
	} elsif ($Details-&gt;{Dual_User_Online}) {
		#run the DualUserError hook 9,10
		if (exists $Me-&gt;{_Hooks}{$_-&gt;{9}{10}} ) {
			&amp;{$Me-&gt;{_Hooks}{9}{10}}($Me, $_)
		}
	} else
	{
	}

	if($Me-&gt;{_LoggedIn}) {
		$Me-&gt;SetError("Disconnected by server");
	}
	return 0;
}


sub File_Handle_Incoming
{
	my ($Me,$connection) = @_;
	my $socket = $connection-&gt;{Socket};
	my $select = $connection-&gt;{Select};
	my @Reply = ();
	while (IO::Select-&gt;select($select, undef, undef, .00001)) {
		my $RawPacket;
		$socket-&gt;recv($RawPacket, 10000);
		if (length($RawPacket)==0) {
			$connection-&gt;{Disconnected} = 1;
			last;
		}
		my @Packet = split(//, $RawPacket);
		foreach (@Packet)
		{
			$_ = ord;
		}
		@{$connection-&gt;{Packet}} = (@{$connection-&gt;{Packet}}, @Packet);
		@Packet = @{$connection-&gt;{Packet}};
		my $len = @Packet;

		if($len&gt;2) {
			my $datalen = _endian_bytes_to_int(\@Packet, 0, 2);
			$len = @Packet;
			while($datalen+2 &lt;= $len) # We have Full packet
			{
				if($connection-&gt;{State}==1) {
					my $id = _bytes_to_int(\@Packet, 2, 1);
					if($id != 0xff) {
						SetError("Direct Connection data misalligned!");
						$connection-&gt;{Disconnected} = 1;
						last;
					}
					my $ruin = _endian_bytes_to_int(\@Packet,17,4);
					$connection-&gt;{UIN} = $ruin;
					my $sid = _endian_bytes_to_int(\@Packet,34,4);
					my $port = $Me-&gt;{_FilePort};
					my @tmp = _int_to_endian_bytes(4,$Me-&gt;{_UIN});
					my $ip = inet_aton($Me-&gt;{_DCIP});
					push(@Reply, _int_to_endian_bytes(2,4)); #length
					push(@Reply, _int_to_endian_bytes(4,1)); # 1
					# INIT2
					push(@Reply, _int_to_endian_bytes(2,0x30)); # length
					push(@Reply, _int_to_endian_bytes(1,0xff)); # ID
					push(@Reply, _int_to_endian_bytes(2,0x07)); # Version
					push(@Reply, _int_to_endian_bytes(2,0x2b)); # ???
					push(@Reply, _int_to_endian_bytes(4,$ruin)); # Remote UIN
					push(@Reply, _int_to_endian_bytes(2,0x0)); # ??
					push(@Reply, _int_to_endian_bytes(4,$port)); # Listen port
					push(@Reply, _int_to_endian_bytes(4,$Me-&gt;{_UIN})); # My UIN
					push(@Reply, _str_to_bytes($ip)); # External IP
					# TODO Make Local/External IPs different
					push(@Reply, _str_to_bytes($ip)); # Local IP
					push(@Reply, _int_to_endian_bytes(1,0x04)); # TCP flag
					push(@Reply, _int_to_endian_bytes(4,$port)); # listen port
					push(@Reply, _int_to_endian_bytes(4,$sid)); # sid
					push(@Reply, _int_to_endian_bytes(4,0x00000050)); # ?
					push(@Reply, _int_to_endian_bytes(4,0x00000003)); # ?
					push(@Reply, _int_to_endian_bytes(4,0x00000000)); # ?
					$connection-&gt;{State}++;

				} elsif($connection-&gt;{State} == 2) {
				# INIT_ACK
					my $data = _endian_bytes_to_int(\@Packet, 2, 4);
					if(($datalen != 4) || ($data != 1)) {
						SetError("DC: INIT_ACK failed");
						$connection-&gt;{Disconnected} = 1;
						last;
					}
					$connection-&gt;{State}++;

				} elsif($connection-&gt;{State} == 3) {
				# INIT_ACK
					my $id = _endian_bytes_to_int(\@Packet, 2, 1);
					my $ID = "File:$id";
					# print "\n&gt;&gt;&gt;&gt;&gt; $ID\n";
					print "can't run sub!![$ID]\n" if ( !(exists $_FileDC_Decoder{$ID}) );
					my ($Refined);
					$Refined = &amp;{$_FileDC_Decoder{$ID}}($Me,
						{ Data_Load =&gt; \@Packet ,
						Connection =&gt; $connection} )
						if ( exists $_FileDC_Decoder{$ID} );
						&amp;{$Me-&gt;{_Hooks}{$ID}}($Me, $Refined) if exists $Me-&gt;{_Hooks}{$ID};
				}
				$datalen += 2;
				splice @{$connection-&gt;{Packet}},0,$datalen;
				splice @Packet,0,$datalen;
				my $l = @Packet;
				if(@Packet &gt; 2) {
					$datalen = _endian_bytes_to_int(\@Packet, 0, 2);
				} else {
					$datalen = 0xffffffff;
				}
			}
		}
	}

	if(@Reply) {
		push(@{$connection-&gt;{ToSend}},@Reply);
	}
}

sub File_Handle_Outgoing
{
	my ($Me,$connection) = @_;

	my $select = $connection-&gt;{Select};
	my $socket = $connection-&gt;{Socket};
	# if(IO::Select-&gt;select(undef, $select, undef, .00001)) {
		my $RawData = '';
		foreach (@{$connection-&gt;{ToSend}})
		{
			$RawData .= chr $_;
		}
		my $result = 0;
		eval { $result = $socket-&gt;send($RawData); };
		if(($result &gt; 0) || $@) {
			splice @{$connection-&gt;{ToSend}},0,$result;
		} else {
			$connection-&gt;{Diconnected} = 1;
		}
	# }
}


sub DC_Handle_Incoming
{
	my ($Me,$connection) = @_;
	my $socket = $connection-&gt;{Socket};
	my $select = $connection-&gt;{Select};
	my @Reply = ();
	while (IO::Select-&gt;select($select, undef, undef, .00001)) {
		my $RawPacket;
		$socket-&gt;recv($RawPacket, 10000);
		if (length($RawPacket)==0) { $connection-&gt;{Disconnected} = 1;
			last;
		}
		my @Packet = split(//, $RawPacket);
		foreach (@Packet)
		{
			$_ = ord;
		}
		@{$connection-&gt;{Packet}} = (@{$connection-&gt;{Packet}}, @Packet);
		@Packet = @{$connection-&gt;{Packet}};
		my $len = @Packet;

		if($len&gt;2) {
			my $datalen = _endian_bytes_to_int(\@Packet, 0, 2);
			$len = @Packet;
			while($datalen+2 &lt;= $len) # We have Full packet
			{
				if($connection-&gt;{State}==1) {
					my $id = _bytes_to_int(\@Packet, 2, 1);
					if($id != 0xff) {
						SetError("Direct Connection data misalligned!");
						$connection-&gt;{Disconnected} = 1;
						last;
					}
					my $ruin = _endian_bytes_to_int(\@Packet,17,4);
					$connection-&gt;{UIN} = $ruin;
					my $sid = _endian_bytes_to_int(\@Packet,34,4);
					# my $sid = chr ($Packet[34]) . chr ($Packet[35]) . chr ($Packet[36]) . chr ($Packet[37]);
					my $port = $Me-&gt;{_DCPort};
					my @tmp = _int_to_endian_bytes(4,$Me-&gt;{_UIN});
					my $ip = inet_aton($Me-&gt;{_DCIP});
					push(@Reply, _int_to_endian_bytes(2,4)); #length
					push(@Reply, _int_to_endian_bytes(4,1)); # 1
					# INIT2
					push(@Reply, _int_to_endian_bytes(2,0x30)); # length
					push(@Reply, _int_to_endian_bytes(1,0xff)); # ID
					push(@Reply, _int_to_endian_bytes(2,0x07)); # Version
					push(@Reply, _int_to_endian_bytes(2,0x2b)); # ???
					push(@Reply, _int_to_endian_bytes(4,$ruin)); # Remote UIN
					push(@Reply, _int_to_endian_bytes(2,0x0)); # ??
					push(@Reply, _int_to_endian_bytes(4,$port)); # Listen port
					push(@Reply, _int_to_endian_bytes(4,$Me-&gt;{_UIN})); # My UIN
					push(@Reply, _str_to_bytes($ip)); # External IP
					# TODO Make Local/External IPs different
					push(@Reply, _str_to_bytes($ip)); # Local IP
					push(@Reply, _int_to_endian_bytes(1,0x04)); # TCP flag
					push(@Reply, _int_to_endian_bytes(4,$port)); # listen port
					push(@Reply, _int_to_endian_bytes(4,$sid)); # sid
					push(@Reply, _int_to_endian_bytes(4,0x00000050)); # ?
					push(@Reply, _int_to_endian_bytes(4,0x00000003)); # ?
					push(@Reply, _int_to_endian_bytes(4,0x00000000)); # ?
					$connection-&gt;{State}++;

				} elsif($connection-&gt;{State} == 2) {
				# INIT_ACK
					my $data = _endian_bytes_to_int(\@Packet, 2, 4);
					if(($datalen != 4) || ($data != 1)) {

						SetError("DC: INIT_ACK failed");
						$connection-&gt;{Disconnected} = 1;
						last;
					}
					$connection-&gt;{State}++;

				} elsif($connection-&gt;{State} == 3) {
				# INIT_ACK
					my $id = _endian_bytes_to_int(\@Packet, 2, 1);
					my $data = _endian_bytes_to_int(\@Packet, 7, 4);
					if(($datalen != 0x21) || ($id != 3) || ($data != 0x1)) {
						# open FILE,"&gt; packet";
						# foreach (@Packet)
						# {
							# print FILE chr($_);
						# }
						# close FILE;
						$connection-&gt;{Disconnected} = 1;
						last;
					}
					push(@Reply, _int_to_endian_bytes(2,0x21)); #length
					push(@Reply, _int_to_endian_bytes(1,3)); # 3
					push(@Reply, _int_to_endian_bytes(4,0x0a)); # ??
					push(@Reply, _int_to_endian_bytes(4,0x01)); # ??
					push(@Reply, _int_to_endian_bytes(4,0x01)); # ??
					push(@Reply, _int_to_endian_bytes(4,0x00)); # ??
					push(@Reply, _int_to_endian_bytes(4,0x00)); # ??
					push(@Reply, _int_to_endian_bytes(4,0x00040001)); # ??
					push(@Reply, _int_to_endian_bytes(4,0x00)); # ??
					push(@Reply, _int_to_endian_bytes(4,0x00)); # ??
					$connection-&gt;{State}++;

				} else {
					my $id = _endian_bytes_to_int(\@Packet, 2, 1);
					my $ID = "DC:$id";
					if (exists $Me-&gt;{_Hooks}{$ID} ) {

							#decode the Sub_ID etc..
							if(!(@Packet = Decrypt(\@Packet))) {
								return -1;
							}

							my $cmd = _endian_bytes_to_int(\@Packet, 7, 2);
							# my $type = _endian_bytes_to_int(\@Packet, 25, 1);
							my $dcID = "DC:$cmd";
							print "can't run sub!![$dcID]\n" if ( !(exists $_DC_Decoder{$dcID}) );
							my ($Refined);
							$Refined = &amp;{$_DC_Decoder{$dcID}}($Me, { Data_Load =&gt; \@Packet , Connection =&gt; $connection} )
								if ( exists $_DC_Decoder{$dcID} );
							&amp;{$Me-&gt;{_Hooks}{$ID}}($Me, $Refined);
					}
				}
				$datalen += 2;
				splice @{$connection-&gt;{Packet}},0,$datalen;
				splice @Packet,0,$datalen;
				my $l = @Packet;
				if(@Packet &gt; 2) {
					$datalen = _endian_bytes_to_int(\@Packet, 0, 2);
				} else {
					$datalen = 0xffffffff;
				}
			}
		}
	}

	if(@Reply) {
		push(@{$connection-&gt;{ToSend}},@Reply);
	}
}

sub DC_Handle_Outgoing
{
	my ($Me,$connection) = @_;

	my $select = $connection-&gt;{Select};
	my $socket = $connection-&gt;{Socket};
	# if(IO::Select-&gt;select(undef, $select, undef, .00001)) {
		my $RawData = '';
		foreach (@{$connection-&gt;{ToSend}})
		{
			$RawData .= chr $_;
		}
		my $result = 0;
		eval { $result = $socket-&gt;send($RawData); };
		if(($result &gt; 0) || $@) {
			splice @{$connection-&gt;{ToSend}},0,$result;
		} else {
			$connection-&gt;{Diconnected} = 1;
		}
	# }
}

sub HandleDC
{
	my ($Me) = @_;
	# my $connections = @{$Me-&gt;{_DCs}};
	my $sock;
	# Terminal problems with 5.00503 IO::Socket::INET-&gt;accept
	# its just a fragment from Socket.pm of 5.6.0
	my $sel = new IO::Select $Me-&gt;{_DCSocket};
	if($sel-&gt;can_read(0.005)) {
		if($sock = $Me-&gt;{_DCSocket}-&gt;accept()) {
			push @{$Me-&gt;{_DCs}},{
				Socket =&gt; $sock,
				Select =&gt; IO::Select-&gt;new($sock),
				State =&gt; 1,	# 1 - Waiting for INIT
						# 2 - Waiting for INIT_ACK
						# 3 - Waiting for INIT2
						# 4 - Waiting for INIT2_ACK
				Packet =&gt; []};
				# print "\nDirect connection from ", $sock-&gt;peerhost(), ':' , $sock-&gt;peerport();
			# print "\n";
		} else {
			$@ = undef;
		}
	}

	my $connection = undef;
	my @tmp = ();
	foreach $connection (@{$Me-&gt;{_DCs}})
	{
		if(!$connection-&gt;{Disconnected}) {
			# TODO Make DC.pm
			$Me-&gt;DC_Handle_Incoming($connection);
			my $size = @{$connection-&gt;{ToSend}};
			$Me-&gt;DC_Handle_Outgoing($connection);
			push @tmp,$connection;
		}
	}
	@{$Me-&gt;{_DCs}} = @tmp;
	# cleanup
}

sub HandleFileDC
{
	my ($Me) = @_;
	my $sock;
	# Terminal problems with 5.00503 IO::Socket::INET-&gt;accept
	# its just a fragment from Socket.pm of 5.6.0
	my $sel = new IO::Select $Me-&gt;{_FileSocket};
	if($sel-&gt;can_read(0.005)) {
		if($sock = $Me-&gt;{_FileSocket}-&gt;accept()) {
			push @{$Me-&gt;{_FileDCs}},{
				Socket =&gt; $sock,
				Select =&gt; IO::Select-&gt;new($sock),
				State =&gt; 1,	# 1 - Waiting for INIT
						# 2 - Waiting for INIT_ACK
						# 3 - Waiting for INIT2
						# 4 - Waiting for INIT2_ACK
				Packet =&gt; []};
		} else {
			$@ = undef;
		}
	}

	my $connection = undef;
	my @tmp = ();
	foreach $connection (@{$Me-&gt;{_FileDCs}})
	{
		if(!$connection-&gt;{Disconnected}) {
			# TODO Make DC.pm
			$Me-&gt;File_Handle_Incoming($connection);
			my $size = @{$connection-&gt;{ToSend}};
			$Me-&gt;File_Handle_Outgoing($connection);
			push @tmp,$connection;
		}
	}
	@{$Me-&gt;{_FileDCs}} = @tmp;
}


sub Check_Incoming
{
	my ($Me) = @_;
	my($RawPacket, @Packet,$http_status);

	while (IO::Select-&gt;select($Me-&gt;{_Select}, undef, undef, .00001)) {
		$Me-&gt;{_Socket}-&gt;recv($RawPacket, 10000);

		if (!$RawPacket) {
			# print "==&gt; ",POSIX::errno(),"\n";
			# print "recv error: $!\n";
			$Me-&gt;SetError("recv error");
			$Me-&gt;Disconnect;
			return;
		}
		# @Packet = split(//, $RawPacket);
		# print "&gt;&gt;",length($RawPacket), "\n";

		# open F,"&gt;&gt; flow";
		# print F $RawPacket;
		# close F;

		if( $Me-&gt;{_Proxy_Type} eq 'https' &amp;&amp; $RawPacket =~ /^HTTP\/1\.[01]\s+(\d+)/) {
			$http_status = $1;
			if ( $http_status != 200 ) {
				$Me-&gt;SetError( "HTTP proxy error: status $http_status");
				$Me-&gt;Disconnect;
				return;
			} else {
				$RawPacket =~ s/^HTTP.*(\r?\n)\1//s;
			}
		}
		@Packet = split(//, $RawPacket);

		foreach (@Packet) {
			$_ = ord;
		}

		my $PLength = @Packet;
		#decode the packet into FLAPs
		for(my $i =0; $i &lt; $PLength; $i++) {

			if ($Me-&gt;{_FLAP_Bytes_Left} &gt; 0) {
				if($Me-&gt;{_FLAP_SubHeader_Bytes_Left} &gt; 0) {
					push @{$Me-&gt;{_FLAP_Sub_Header}}, $Packet[$i];
					$Me-&gt;{_FLAP_SubHeader_Bytes_Left}--;
					if (!$Me-&gt;{_FLAP_SubHeader_Bytes_Left}) {
						my (@HeaderPacket);
						@HeaderPacket = @{$Me-&gt;{_FLAP_Sub_Header}};
						$Me-&gt;{_FLAP_In_progress}{Family_ID}	 = _bytes_to_int(\@HeaderPacket, 0, 2);
						$Me-&gt;{_FLAP_In_progress}{Sub_ID}		= _bytes_to_int(\@HeaderPacket, 2, 2);
					}
				}
				push (@{$Me-&gt;{_FLAP_In_progress}{Data_Load}}, $Packet[$i]);

				$Me-&gt;{_FLAP_Bytes_Left}--;

				if ($Me-&gt;{_FLAP_Bytes_Left} &lt;= 0) {
					#end the FLAP, and move it to the Queue..
					push(@{$Me-&gt;{_Incoming_Queue}}, $Me-&gt;{_FLAP_In_progress});
					$Me-&gt;{_FLAP_In_progress} = undef;
					$Me-&gt;{_FLAP_Header} = [];
					$Me-&gt;{_FLAP_Sub_Header} = [];
					$Me-&gt;{_FLAP_Bytes_Left} = 0;
					$Me-&gt;{_FLAP_SubHeader_Bytes_Left} = 4;
					$Me-&gt;{_FLAP_Header_Bytes_Left} = 6;
				}
				next;
			}

			#it's a new FLAP.. or part of new....
			# _print_packet($RawPacket, ());
				# print ".",$Me-&gt;{_FLAP_Header_Bytes_Left};

			if($Me-&gt;{_FLAP_Header_Bytes_Left} &gt; 0) {
				push @{$Me-&gt;{_FLAP_Header}}, $Packet[$i];
				$Me-&gt;{_FLAP_Header_Bytes_Left}--;
				next if $Me-&gt;{_FLAP_Header_Bytes_Left};
			}

			my (@HeaderPacket);
			@HeaderPacket = @{$Me-&gt;{_FLAP_Header}};
			if($HeaderPacket[0] != 42 ) {
				$Me-&gt;SetError("Received Data Missaligned!");
				return;
			}

			$Me-&gt;{_FLAP_In_progress}{Channel_ID}	= _bytes_to_int(\@HeaderPacket, 1, 1);
			$Me-&gt;{_FLAP_In_progress}{Sequence_ID}	= _bytes_to_int(\@HeaderPacket, 2, 2);
			$Me-&gt;{_FLAP_In_progress}{Data_Size}	 = $Me-&gt;{_FLAP_Bytes_Left} = _bytes_to_int(\@HeaderPacket, 4, 2);
			# $Me-&gt;{_FLAP_In_progress}{Family_ID}	 = _bytes_to_int(\@HeaderPacket, 6, 2);
			# $Me-&gt;{_FLAP_In_progress}{Sub_ID}		= _bytes_to_int(\@HeaderPacket, 8, 2);

			# $i--;
		}
	}
}

sub Deal_With_FLAPs
{
	my($Me) = @_;
	foreach (@{$Me-&gt;{_Incoming_Queue}}) {
		if ($_-&gt;{Channel_ID} == 1) {
			my $ID = $_-&gt;{Family_ID}.":".$_-&gt;{Sub_ID};
			#login system message, deal with it..
			if ( exists $_New_Connection_Nefotiation_Codes{$_-&gt;{Sub_ID}} ) {
				# print "Found Connection Event, Dealing with it,,\n";
				&amp;{$_New_Connection_Nefotiation_Codes{$_-&gt;{Sub_ID}}}($Me, $_);
			}

		} elsif ($_-&gt;{Channel_ID} == 2) {
			#This is a non critical FLAP. so decode, and pass to a hook if there is one..
			my $ID = $_-&gt;{Family_ID}.":".$_-&gt;{Sub_ID};

				print "zIncoming..($ID)\n" if $Me-&gt;{_Debug};
				_print_packet($_-&gt;{Data_Load}, ()) if $Me-&gt;{_Debug};

			if (exists $Me-&gt;{_Hooks}{$ID} ) {

				#decode the Sub_ID etc..
				print "can't run sub!![$ID]\n" if ( !(exists $_Srv_Decoder{$ID}) );
				print "&gt;&gt;&gt; $ID\n" if $Me-&gt;{_Debug};

				my ($Refined);

				$Refined = &amp;{$_Srv_Decoder{$ID}}($Me, $_)	if ( exists $_Srv_Decoder{$ID} );

				if((!exists $Refined-&gt;{MessageType})
					|| ($Refined-&gt;{MessageType} eq ''))
				{
					$Refined-&gt;{MessageType} = "$ID";
				}
				#run the Hook..
				&amp;{$Me-&gt;{_Hooks}{$ID}}($Me, $Refined);
			} elsif ($Me-&gt;{_Auto_Login}) {
				&amp;{$_Srv_Decoder{$ID}}($Me, $_)	if ( exists $_Srv_Decoder{$ID} );
			} elsif ($Me-&gt;{_Debug}) {
				print "zzzIncoming..\n" if $Me-&gt;{_Debug};
				print "[".$_-&gt;{Channel_ID}."][".$_-&gt;{Sequence_ID}."][".$_-&gt;{Data_Size}."][".$_-&gt;{Family_ID}."][".$_-&gt;{Sub_ID}."]\n" if $Me-&gt;{_Debug};
				_print_packet($_-&gt;{Data_Load}, ()) if $Me-&gt;{_Debug};
			}
		} elsif ($_-&gt;{Channel_ID} == 4) {
			# print "Found DisConnection Event, Dealing with it,,\n";
			print "." unless $Me-&gt;{_LoggedIn};
			my $ret = &amp;_Disconnection_Nefotiation($Me, $_);
			return if($ret);
		} elsif ($_-&gt;{Channel_ID} == 5) {
			print "&gt;&gt; im alive! &lt;&lt;\n";
		} else {
			#this is an error type message..
		}

	}
	$Me-&gt;{_Incoming_Queue} = [];
}

sub Send_Outgoing
{
	my($Me) = @_;
	my($Chan, $Data_Size, @Header, $Raw_Data) = ();

	foreach (@{$Me-&gt;{_Outgoing_Queue}}) {

		if ($_-&gt;{Channel_ID}){$Chan = $_-&gt;{Channel_ID};} else {$Chan = 2;}
 		if(ref($_-&gt;{Data_Load}) eq "ARRAY") {
			$Data_Size = @{$_-&gt;{Data_Load}};
 		}
 		else {
 			$Data_Size = 0;
 		}

		@Header = (42, $Chan);

		$Me-&gt;{_Seq_Num}++;
		$Me-&gt;{_Seq_Num} = 0 if $Me-&gt;{_Seq_Num} &gt; 65535;

		push(@Header, _int_to_bytes(2, $Me-&gt;{_Seq_Num}));
		push(@Header, _int_to_bytes(2, $Data_Size));

		foreach (@Header)
		{
			$Raw_Data .= chr($_);
		}

		foreach (@{$_-&gt;{Data_Load}})
		{
			$Raw_Data .= chr($_);
		}

		print "Outgoing..\n" if $Me-&gt;{_Debug};
		_print_packet(\@Header, \@{$_-&gt;{Data_Load}}) if $Me-&gt;{_Debug};

	}

	#send them all off..
	if ($Raw_Data) {
		my $res = $Me-&gt;{_Socket}-&gt;send($Raw_Data);
	}

	$Me-&gt;{_Outgoing_Queue} = [];
}

#########################
### Private functions ###
#########################

#These functions should only ever be run from within the ICQ object..


sub xml_sub
{
	my $char = shift;
	return '&amp;amp;' if($char eq '&amp;');
	return '&amp;lt;' if($char eq '&lt;');
	return '&amp;gt;' if($char eq '&gt;');
	return '&amp;quot;' if($char eq '"');
	return '&amp;apos;' if($char eq "'");
	return $char;
}

sub xml_escape
{
	my $s = shift;
	$s=~s/([&amp;&lt;&gt;"'])/&amp;xml_sub($1)/eg;
	return $s;
}



# _bytes_to_int(array_ref, start, bytes)
#
# Converts the byte array referenced by &lt;array_ref&gt;, starting at offset
# &lt;start&gt; and running for &lt;bytes&gt; values, into an integer, and returns it.
# The bytes in the array must be in little-endian order.
#
# _bytes_to_int([0x34, 0x12, 0xAA, 0xBB], 0, 2) == 0x1234
# _bytes_to_int([0x34, 0x12, 0xAA, 0xBB], 2, 1) == 0xAA

sub _endian_bytes_to_int
{
	my ($array, $start, $bytes) = @_;
	my ($ret);

	$ret = 0;
	for (my $i = $start+$bytes-1; $i &gt;= $start; $i--) {
		$ret &lt;&lt;= 8;
		$ret |= ($array-&gt;[$i] or 0);
	}

	return $ret;
}

sub _bytes_to_int
{
	my ($array, $start, $bytes) = @_;
	my ($ret);

	$ret = 0;
	for (my $i = $start; $i &lt; $start+$bytes; $i++) {
		$ret &lt;&lt;= 8;
		$ret |= ($array-&gt;[$i] or 0);
	}
	return $ret;
}

# _int_to_endian_bytes(bytes, val)
#
# Converts &lt;val&gt; into an array of &lt;bytes&gt; bytes and returns it.
# If &lt;val&gt; is too big, only the &lt;bytes&gt; least significant bytes are
# returned. The array is in little-endian order.
#
# _int_to_bytes(2, 0x1234) == (0x34, 0x12)
# _int_to_bytes(2, 0x12345) == (0x45, 0x23)

sub _int_to_endian_bytes
{
	my ($bytes, $val) = @_;
	my (@ret);

	for (my $i=0; $i&lt;$bytes; $i++) {
		push @ret, ($val &gt;&gt; ($i*8) &amp; 0xFF);
	}
	return @ret;
}

# _int_to_bytes(bytes, val)
#
# Converts &lt;val&gt; into an array of &lt;bytes&gt; bytes and returns it.
# If &lt;val&gt; is too big, only the &lt;bytes&gt; least significant bytes are
# returned. The array is not little-endian order.
#
# _int_to_bytes(2, 0x1234) == (0x12, 0x34)
# _int_to_bytes(2, 0x12345) == (0x12, 0x34)

sub _int_to_bytes
{
	my ($bytes, $val) = @_;
	my (@ret);
	$val = 0 if not defined $val;
	for (my $i=0; $i&lt;$bytes; $i++) {
		unshift @ret, ($val &gt;&gt; ($i*8) &amp; 0xFF);
	}
	return @ret;
}

# _str_to_bytes(str, add_zero)
#
# Converts &lt;str&gt; into an array of bytes and returns it.
#
# _str_to_bytes('foo')	 == ('f', 'o', 'o')

sub _str_to_bytes
{
	my ($string) = @_;
	my (@ret);

	# the ?: keeps split() from complaining about undefined values
	foreach (split(//, defined($string) ? $string : '')) {
		push @ret, ord($_);
	}
	return @ret;
}

sub _bytes_to_lnts
{
	my ($array, $start) = @_;
	my $DataLength = _endian_bytes_to_int($array, $start ,2);
	my $text = _bytes_to_str($array,$start+2 ,$DataLength-1);
	return $text;
}

sub _str_to_lnts
{
	my ($string) = (@_);
	my (@ret);
	push(@ret, _int_to_endian_bytes(2, length($string)+1));
	push(@ret, _str_to_bytes($string));
	push(@ret, _int_to_bytes(1, 0x00));

	return @ret;
}

sub _str_to_bws
{
	my ($string) = (@_);
	my (@ret);
	push(@ret, _int_to_bytes(2, length($string)));
	push(@ret, _str_to_bytes($string));

	return @ret;
}



# _uin_to_buin(str, add_zero)
#
# Converts &lt;str&gt; into an array of bytes and returns it.
#
# _str_to_bytes('foo')	 == ('f', 'o', 'o')

sub _uin_to_buin
{
	my ($uin) = @_;
	my (@ret);
	push @ret, length($uin);
	# the ?: keeps split() from complaining about undefined values
	foreach (split(//, defined($uin) ? $uin : '')) {
		push @ret, ord($_);
	}

	return @ret;
}


# _bytes_to_str(array_ref, start, bytes)
#
# Converts the byte array referenced by &lt;array_ref&gt;, starting at offset
# &lt;start&gt; and running for &lt;bytes&gt; values, into a string, and returns it.
#
# _bytes_to_str([0x12, 'f', 'o', 'o', '!'], 1, 3) == 'foo'


sub _bytes_to_str
{
	# thanks to Dimitar Peikov for the fix
	my ($array, $start, $bytes) = @_;
	my $ret='';

	for (my $i = $start; $i &lt; $start+$bytes; $i++) {
		next if(!defined($array-&gt;[$i]));
		$ret .= ($array-&gt;[$i] ne '') ? chr($array-&gt;[$i]) : '';
	}

	return $ret;
}

sub _bytes_to_ds
{
	# thanks to Dimitar Peikov for the fix
	my ($array, $start) = @_;
	my ($ret);

	my $bytes = _endian_bytes_to_int($array, $start, 4);
	$start += 4;

	$ret = '';
	for (my $i = $start; $i &lt; $start+$bytes; $i++) {
		$ret .= ($array-&gt;[$i] ne '') ? chr($array-&gt;[$i]) : '';
	}

	return ($ret, $start + $bytes) ;
}




# print_packet(Header_packet_ref, Body_packet_ref)
#
# Dumps the ICQ packet contained in the byte array referenced by
# &lt;packet_ref&gt; to STDOUT.

sub _print_packet
{
	my ($Header, $packet) = @_;
	my ($Counter, $TLine);

	foreach (@$Header) {
		$Counter++;

		print sprintf("%02X ", $_);

		if ($_ &gt;= 32) {
			$TLine .= chr($_);
		} else {
			$TLine .= ".";
		}

		if ($Counter % 16 == 0) {
			print "  ".$TLine."\n";
			$TLine = '';
		}
	}
	while ($Counter &gt; 16){$Counter -=16}

	if (16 - $Counter &gt; 1 &amp;&amp; $Counter &gt; 0) {
		foreach (1..(16 - $Counter)) {
			print "   ";
		}
		print "  ".$TLine."\n";
	}
	$TLine ='';
	$Counter =0;

	foreach (@$packet) {
		$Counter++;

		print sprintf("%02X ", $_);

		if ($_ &gt;= 32) {
			$TLine .= chr($_);
		} else {
			$TLine .= ".";
		}

		if ($Counter % 16 == 0) {
			print "  ".$TLine."\n";
			$TLine = '';
		}
	}
	while ($Counter &gt; 16){$Counter -=16}

	if (16 - $Counter &gt; 1 &amp;&amp; $Counter &gt; 0) {
		foreach (1..(16 - $Counter)) {
			print "   ";
		}
		print "  ".$TLine."\n";
	}
	print "\n";
}

# _Password_Encrypt(Password_String)
# Encrypts the password for sending to the server using a simple XOR "encryption" method
sub _Password_Encrypt
{
	my ($Password) = @_;
	my ($FinishedString);

	my @Pass = split (//, $Password);

	foreach (@Pass) {
		$_ = ord($_);
	}

	my @encoding_table = (
		0xf3, 0x26, 0x81, 0xc4,
		0x39, 0x86, 0xdb, 0x92,
		0x71, 0xa3, 0xb9, 0xe6,
		0x53, 0x7a, 0x95, 0x7c);

	for (my $i = 0; $i &lt; length($Password); $i++){
		$FinishedString .= chr($Pass[$i] ^ $encoding_table[$i]);
	}

	return ($FinishedString);
}

#
# Encryption function for direct connection
#

sub Encrypt
{
	my($aref) = @_;
	my $size = @{$aref};
	my @out = ();
	push @out, _int_to_endian_bytes(2,$size+1);
	push @out, _int_to_endian_bytes(1,0x02);
	my $M1 = (rand(900) % (($size &lt; 255 ? $size : 255)-10))+10;
	my $X1 = ${$aref}[$M1] ^ 0xff;
	my $X2 = rand(900) % 220;
	my $X3 = $client_check_data[$X2] ^ 0xFF;
	my $B1 = (${$aref}[4]&lt;&lt;24)|(${$aref}[6]&lt;&lt;16)|(${$aref}[4]&lt;&lt;8)|(${$aref}[6]);

	my $check = ($M1 &lt;&lt; 24) | ($X1 &lt;&lt; 16) | ($X2 &lt;&lt; 8) | $X3;

	$check ^= $B1;
	push @out, _int_to_endian_bytes(4, $check);
	my $key = Math::BigInt-&gt;new(1734701672);
	my $tmp = Math::BigInt-&gt;new(4294967296);
	$key *= $size;
	$key += $check;
	$key = $key-&gt;bmod($tmp);
	splice @{$aref},0,4;
	my ($i,$k,$hex,$j);
	for($i=4; $i&lt;int (($size+3)/4); $i+=4) {
		$hex = $key + $client_check_data[$i&amp;0xFF];
		for($j=0;$j&lt;4;$j++)
		{
			$k = shift @{$aref};
			push @out, ($k ^ (($hex&gt;&gt;(8*$j))&amp;0xFF));
		}
	}
	push @out,@{$aref};
	return @out;
}



#
# Decryption function for direct connection
#
sub Decrypt
{
	my($aref) = @_;
	my @out;
	my ($j, $k, $i, $hex);
	my $correction = 3; #v7
	my $size = @{$aref} - $correction;

	my $t1 = shift @{$aref};


	push @out, $t1;
	$t1 = shift @{$aref};
	push @out, $t1;
	$t1 = shift @{$aref};
	push @out, $t1;
	my $check = _endian_bytes_to_int($aref, 0, 4);
	my $check2 = _bytes_to_int($aref, 0, 4);
	splice @{$aref},0,4;
	push @out, _int_to_endian_bytes(4, $check);
	my $key = Math::BigInt-&gt;new(1734701672);
	my $tmp = Math::BigInt-&gt;new(4294967296);
	$key *= $size;
	$key += $check;
	$key = $key-&gt;bmod($tmp);

	for($i=4; $i&lt;int (($size+3)/4); $i+=4) {
		$hex = $key + $client_check_data[$i&amp;0xFF];
		for($j=0;$j&lt;4;$j++)
		{
			$k = shift @{$aref};
			my $zz =($k ^ (($hex&gt;&gt;(8*$j))&amp;0xFF));
			push @out, $zz;
		}
	}
	push @out, @{$aref};
	my $B1 = ($out[4+$correction]&lt;&lt;24) | ($out[6+$correction]&lt;&lt;16) | ($out[4+$correction]&lt;&lt;8) | ($out[6+$correction]);
	# special decryption
	$B1 ^= $check;

	# validate packet
	my $M1 = ($B1 &gt;&gt; 24) &amp; 0xFF;
	if($M1 &lt; 10 || $M1 &gt;= $size) { return (); }

	my $X1 = $out[$M1+$correction] ^ 0xFF;
	if((($B1 &gt;&gt; 16) &amp; 0xFF) != $X1) { return (); }

	my $X2 = (($B1 &gt;&gt; 8) &amp; 0xFF);
	if($X2 &lt; 220) {
		my $X3 = $client_check_data[$X2] ^ 0xFF;
		if(($B1 &amp; 0xFF) != $X3) { return (); }
	}
	# for($i=0;$i&lt;@out;$i++)
	# {
		# ${$aref}[$i] = $out[$i];
	# }
	return @out;
}


# _Make_SNAC_Header(Comand_Family, Sub_Family, FlagA, FlagB, RequestID)
#makes the SNAC header which has to be at the top of every command..

sub _Make_SNAC_Header
{
	my($Family, $Sub_Family, $FlagA, $FlagB, $RequestID) = @_;
	my (@Header);

		@Header = _int_to_bytes(2, $Family);
	push(@Header, _int_to_bytes(2, $Sub_Family));
	push(@Header, _int_to_bytes(1, $FlagA));
	push(@Header, _int_to_bytes(1, $FlagB));
	push(@Header, _int_to_bytes(4, $RequestID));

	return @Header;
}


sub base64encode
{
	my $w = shift;
	my $l = length($w);
	my $res = '';

	my @base64 = qw(A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
					a b c d e f g h i j k l m n o p q r s t u v w x y z
					0 1 2 3 4 5 6 7 8 9 + /);
	while($w =~ /(..?.?)/g) {
		my $b = $1;
		while(length($b)&lt;3)
		{
			$b .= '\x00';
		}
		my @data = split //,$b;
		my $int=0;
		my $mult = 0x10000;
		foreach (@data)
		{
			$_ = ord;
		}

		$int = $data[0]*0x10000 + $data[1]*0x100 + $data[2];

		my $i = 4;
		my @b64 = ('','','','');
		while($i)
		{
			$b64[$i] = $base64[$int % 0x40];
			$int = ($int - $int % 0x40) / 0x40;
			if((@data == 1) &amp;&amp; ( $i &gt; 2)) {
				$b64[$i] = '=';
			}
			if((@data == 2) &amp;&amp; ( $i &gt; 3)) {
				$b64[$i] = '=';
			}
			$i--;
		}
		$res .= join ('',@b64);

	}
	return $res;
}

# this function takes a tagged string (like the server sends..) and
# breaks it up into it's parts...

sub _Decode_Tagged_Text
{
	my($String, $Details) = @_;
	my($Key, $Data, $i);
	$String =~ s/\n/ /g;
	if($String =~ /&lt;([^&gt;]*)&gt;/i) {
		$Details-&gt;{MessageType} = $1;
		if($String =~ /&lt;$Details-&gt;{MessageType}&gt;(.*)&lt;\/$Details-&gt;{MessageType}&gt;?/i) {
			my $keys = $1;
			while($keys =~ /&lt;(.*?)&gt;(.*?)&lt;\/.*?&gt;/g)
			{
				$Details-&gt;{$1} = $2;
			}
		} else {
			$Details-&gt;{MessageType} = 'Invalid data!';
		}
	} else {
		$Details-&gt;{MessageType} = 'Invalid tagged message';
	}
	return ($Details);
}

%_Error_Codes = (
	1 =&gt; "Bad UIN",
	5 =&gt; "Password mismatch",
	24 =&gt; "Connection rate exceeded",
	29 =&gt; "Trying to reconnect too fast"
);



#####################
### TLV functions ###
#####################

# TLV (Type, Length, Value) is the way much of the data is sent an received
# The Data below contains the definitions of the Types, their lengths, and what kind
# of data is to be expected (eg strings or ints etc..)
# Also has the _Write_TLV and _Read_TLV functions..

#definitions for the TLVs types being sent from the server..
#The first digit (2 or 4) denotes the FLAP's Chan
%_TLV_IN = (
	2 =&gt; {	User_Class =&gt;		0x01,#!?????
		Message_Block =&gt;	0x02,# According to ethereal
		SignOn_Date =&gt;		0x03,#!
		Autoresponse =&gt;		0x04, #! Autoresponse flag
		Encoded_Message =&gt;	0x05,#!
		Status =&gt;		0x06,#!
		Ip_Address =&gt;		0x0a,#! in 4 byte format..
		Web_Address =&gt;		0x0b,#!
		LANInfo =&gt;		0x0c,#! (long like 25 bytes..)
		CapabilityInfo =&gt;	0x0d,#! ???
		Time_Online =&gt;		0x0f, #!
		Unknown13   =&gt;		0x13  #
		},
	4 =&gt; {  UIN =&gt;			0x01,#!
		HTML_Address =&gt;		0x04,#!
		Server_And_Port =&gt;	0x05,#!
		Connection_Cookie =&gt;	0x06,#!
		Error_Code =&gt;		0x08,#!
		Dual_User_Online =&gt;	0x09,
		},

	);

#definitions for the TLVs types being sent from us to the server..
#The first digit (1 or 2) denotes the FLAP's Chan
%_TLV_OUT = (
	1 =&gt; {	UIN =&gt;			0x01,#!
		Password =&gt;		0x02,#!
		ClientProfile =&gt;	0x03,#!
		User_Info =&gt;		0x05,
		Connection_Cookie =&gt;	0x06,#!
		CountryCode =&gt;		0x0e,#!
		Language =&gt;		0x0f,#!
		ClientBuildMinor =&gt;	0x14,#!
		ClientType =&gt;		0x16,#!
		ClientVersionMajor =&gt;	0x17,#!
		ClientVersionMinor =&gt;	0x18,#!
		ClientICQNumber =&gt;	0x19,#!
		ClientBuildMajor =&gt;	0x1a #!
	},
	2 =&gt; {	Status =&gt;			0x06,#!
		ErrorCode =&gt;			0x08,#!????
		DirectConnnectionInfo =&gt;	0x0c#!????
	}
);

#if the TLV is a number, we define the number of bytes to use..(note all numbers are their decimal value, not hex)
# 1000 denotes a "raw" data input, and is encoded differently..
%_TLV_Length_O = (
	1 =&gt; {	6 =&gt;	1000,
		20 =&gt;	4,
		22 =&gt;	2,
		23 =&gt;	2,
		24 =&gt;	2,
		25 =&gt;	2,
		26 =&gt;	2
	},
	2 =&gt; {	6 =&gt;	4,
		8 =&gt;	2,
	},
);

#This defines the type of data we expect coming in, the codes are as follows..
# 0 or no entry = String
# 1 = Int
# 2 = Raw (obtains the data still as a string of numbers separated by spaces)
# 3 = IP

%_TLV_Length_I = (
	2 =&gt; {	1 =&gt;	1,
		2 =&gt;	1,
		3 =&gt;	1,
		4 =&gt;	1,
		5 =&gt;	2,
		6 =&gt;	1,
		10 =&gt;	3,
		12 =&gt;	0x25,
		15 =&gt;	1,
	},
	4 =&gt; {	8 =&gt;	1,
		6 =&gt;	2,
	},
);

# _Write_TLV(Message_Channel, Type_Value, Info_To_Encode)
#
# This creates an packet array ready for sending to the server, containing the given data

sub _Write_TLV
{
	my($Chan, $Value, $Infomation) = @_;
	my(@Data);

	$Value = $_TLV_OUT{$Chan}{$Value} if (exists $_TLV_OUT{$Chan}{$Value});
	@Data = _int_to_bytes(2, $Value);
	if (exists $_TLV_Length_O{$Chan}{$Value}) {
		if ($_TLV_Length_O{$Chan}{$Value} == 1000) {
			#get it as an array!
			my @Cookie = split(/ /, $Infomation);
			my $CLength = @Cookie;
			push(@Data, _int_to_bytes(2, $CLength));
			push(@Data, @Cookie);
		} else {
			#their a number, and need a set byte size..
			push(@Data, _int_to_bytes(2, $_TLV_Length_O{$Chan}{$Value}));
			push(@Data, _int_to_bytes($_TLV_Length_O{$Chan}{$Value}, $Infomation));
		}
	} else {
		push(@Data, _int_to_bytes(2, length($Infomation)));
		push(@Data, _str_to_bytes($Infomation));
	}
	return (@Data);
}

# _Read_TLV(Array_to_Read, Message_Channel, Starting_offset_in_array, Array_for_results, Max_number_of_TLVs)
#
# This reads through an packet array picking out and decoding all the TLVs it can find,
# till it reaches the end of the array, or else reaches the Max_Num value (counted in TLVs not bytes..)
# It returns an Hash containing the found types/values and the final of set.

sub _Read_TLV {
	my($Array, $Chan, $Start, $Details, $Max) = @_;
	my($i, $ArrayLength, $DataType, $DataLength, $DataTypeName);

	$ArrayLength = @$Array;

	$Start or $Start = 0;
	$Max or $Max = 100000;

	for ($i = $Start; $i &lt;$ArrayLength;) {
		#only get up to the max number of TVLs
		$Max or last;
		$Max--;

		#read in the Data Type/length..
		$DataType = _bytes_to_int ($Array, $i, 2);
		$DataLength = _bytes_to_int ($Array, $i+2, 2);
		$i += 4;

		#find the name of this data type..
		$DataTypeName = $DataType;
		foreach (keys %{$_TLV_IN{$Chan}}) {
			$DataTypeName = $_ if ($_TLV_IN{$Chan}{$_} == $DataType);
		}

		if( exists $_TLV_Length_I{$Chan}{$DataType}) {
			if ($_TLV_Length_I{$Chan}{$DataType} == 2) {
				#get it as an array!
				for (my $p=0; $p &lt; $DataLength; $p++) {
					$Details-&gt;{$DataTypeName} .= $Array-&gt;[$i+$p]." ";
				}
				chop $Details-&gt;{$DataTypeName};
			} elsif ($_TLV_Length_I{$Chan}{$DataType} == 3) {
				#get it as IP address
				if ($DataLength != 4) {
					print "Argh, This an't an IP!!!\n";
				} else {
					$Details-&gt;{$DataTypeName} = _bytes_to_int ($Array, $i, 1)."."._bytes_to_int ($Array, $i+1, 1)."."._bytes_to_int ($Array, $i+2, 1)."."._bytes_to_int ($Array, $i+3, 1);
				}

			} elsif ($_TLV_Length_I{$Chan}{$DataType} == 1) {
				#we're getting a number...
				$Details-&gt;{$DataTypeName} = _bytes_to_int ($Array, $i, $DataLength);
			} else {
				$Details-&gt;{$DataTypeName} = _bytes_to_str ($Array, $i, $_TLV_Length_I{$Chan}{$DataType});
			}

		} else {
			$Details-&gt;{$DataTypeName} = _bytes_to_str ($Array, $i, $DataLength);
		}
		$i +=$DataLength;
	}
	return ($Details, $i);
}

sub BuildGenericAck
{
	my ($type, $flags, $seqnum, $astatus) = @_;
	$astatus = 0 unless defined $astatus;
	my @Ack = ();
	push(@Ack, _int_to_endian_bytes(4, 0));
	push(@Ack, _int_to_endian_bytes(2, 0x07da));
	push(@Ack, _int_to_endian_bytes(2, 0x000e));
	push(@Ack, _int_to_endian_bytes(2, $seqnum));
	push(@Ack, _int_to_endian_bytes(4, 0));
	push(@Ack, _int_to_endian_bytes(4, 0));
	push(@Ack, _int_to_endian_bytes(4, 0));
	push(@Ack, $type);
	push(@Ack, $flags);
	push(@Ack, _int_to_endian_bytes(2, $astatus)); # status
	push(@Ack, _int_to_endian_bytes(2, 0x0000)); # priority
	return @Ack;

}

sub AcceptFile
{
	my ($Me, $connection) = @_;
	my @Ack;
	my $type = $connection-&gt;{type};
	my $flags = $connection-&gt;{flags};
	my $seqnum = $connection-&gt;{seqnum};
	# Accept ack
	my $port = $Me-&gt;{_FilePort};
	@Ack = BuildGenericAck($type,$flags,$seqnum);
	push(@Ack, _int_to_endian_bytes(1, 0x01));
	push(@Ack, _int_to_bytes(4, $port));
	push(@Ack, _int_to_endian_bytes(2, 0x0000));
	push(@Ack, _str_to_lnts(""));
	push(@Ack, _int_to_bytes(4, 0x12345678));
	push(@Ack, _int_to_endian_bytes(4, $port));
	@Ack = Encrypt(\@Ack);
	push @{$connection-&gt;{ToSend}},@Ack;

}

sub RejectFile
{
	my ($Me, $connection) = @_;
	my $type = $connection-&gt;{type};
	my $flags = $connection-&gt;{flags};
	my $seqnum = $connection-&gt;{seqnum};
	my @Ack;
	@Ack = BuildGenericAck($type,$flags,$seqnum,1);
	push(@Ack, _str_to_lnts("No files!"));
	push(@Ack, _int_to_endian_bytes(4, 0xffffffff));
	push(@Ack, _int_to_endian_bytes(3, 0x000100));
	push(@Ack, _int_to_endian_bytes(4, 0xffffffff));
	push(@Ack, _int_to_endian_bytes(4, 0xffffffff));
	@Ack = Encrypt(\@Ack);
	push @{$connection-&gt;{ToSend}},@Ack;
}

sub GetURLMessage
{
	my ($array, $start) = @_;
	my $DataLength = _bytes_to_int ($array, $start, 2);
	my $data = _bytes_to_str($array, $start+2, $DataLength);
	my @rez = split /\xfe/,$data;
	return @rez;
}

sub _getRequestID {
	my ($self, $type) = @_;
	my $nRequestID = ($self-&gt;{_Mem}*65536+$type);
	$self-&gt;{_Mem}++;
	return $nRequestID;
}

1;

__END__
======================================
19:14 - Server ack - at least for group/user renaming
</pre></body></html>