Checking Domain Availability with PHP

Hi guys! I’m having some trouble making a script to check a domain’s availability… Here’s what I got:

$fp = fsockopen("",43);
while(!feof($fp)) {
echo fgets($fp,1048);
echo "<br />";
} //while

That just tell me the “whois” info, and that’s fine but I want to refine the results to: “It’s availabe” and “Sorry, it’s taken”. Any Ideas will be appreciated!