[PHP] When API will work ?

Since begening I have problems with API.

In log I have:

and:

So API user is not logged off.

My PHP code is:

function blokada_www($typ,$ip,$lista)
{
	include_once('mt/ros_api.php');

	$API = new routeros_api();

	$API->debug = false;

	if ($API->connect('host', 'user', 'pass'))
	{
		if($typ == "usun")
		{
			$API->write('/ip/firewall/address-list/getall');

			$READ = $API->read();
		   $array = $API->parse_response($READ);

			for($x=0;$x<count($array);$x++)
			{
				if(array_search($ip, $array[$x]))
				{
					if($array[$x]["list"] == $lista)
					{
						$API->write('/ip/firewall/address-list/remove',false);
						$API->write('=.id='.$array[$x][".id"],true);
						$odp = $API->read();
						if($odp[0] == "!done") return TRUE;
					}
				}
			}
			$API->disconnect();
		}		
		elseif($typ == "dodaj")
		{
			$API->write('/ip/firewall/address-list/getall');

			$READ = $API->read();
		   $array = $API->parse_response($READ);
			
			for($x=0;$x<count($array);$x++)
			{
				//echo $x."-".$array[$x]["list"]."-".$lista." | ".$array[$x]["address"]."-".$ip."<br>";
			
				if($array[$x]["list"] == $lista && $array[$x]["address"] == $ip)
				{
					//echo "Adres ".$array[$x]["address"]." jest juş na li�cie ".$lista;
					$count++;
					return TRUE;
				}
			}
			
			if($count == 0)
			{
				$API->write('/ip/firewall/address-list/add',false);
				$API->write('=list='.$lista,false);
				$API->write('=address='.$ip);
				$odp = $API->read();
				if($odp[0] == "!done") return TRUE;
			}
			$API->disconnect();
			
		}		
	}
}

My question is:
Why sometimes this works, and somethimes not?

what version of ROS do you use?

API log off is equal to closing connection, if you do not do that proper way, USER will stay logged in if logged through API

also /quit exists

3.30

On every

if

there is

$API->disconnect();

so what else shoud i do?