Cannot access a certain channel via PHP script

Since you guys asked for my script, here it is. Just copy and paste this into a new PHP file, save it as irc.php, then run it from your command line program. I provided the command syntax in the code.

<?php
// enable $argv
ini_set('register_argc_argv', 1);

/*
$argv values
1 — Twitch account
2 — Channel to access
3 — Account token (password)

Use this syntax in your command line to run:
php <path to script> <your username> <channel> <token>
*/

// Text file for login output
$lgnFileLoc = "./login.txt";

if (!$argv[1] || !$argv[2] || !$argv[3]) {
    // log IRC attempt with error
    $lgnFile = fopen($lgnFileLoc, "a");
    fwrite($lgnFile, "Script arguments could not be supplied\r\n");
    fclose($lgnFile);

} else {
    // log IRC with arguments
    $lgnFile = fopen($lgnFileLoc, "a");
    fwrite($lgnFile, "Accessing ".$argv[2]."'s channel as ".$argv[1]." with a token of ".$argv[3]."\r\n");
    fclose($lgnFile);

    // begin IRC
    irc_action($argv[1], $argv[2], $argv[3]);
}

function irc_action($user, $ch, $pass) {

set_time_limit(30);

function IRC($name, $channel, $pwd) {
    $config = array(
            'server'  => 'irc.twitch.tv', 
            'port'    => 6667, 
            'channel' => '#'.$channel,
            'name'    => $name,
            'nick'    => $name,
            'pass'    => 'oauth:'.$pwd
    );

    $chatServer = array();
    $chatServer['connect'] = @fsockopen($config['server'], $config['port'], $errno, $errstr, 15);
    $cs = $chatServer['connect'];

    // log file
    $lgnFileLoc = $GLOBALS['lgnFileLoc'];

    if ($cs) {
        // login success
        fwrite($cs, "PASS " . $config['pass'] . "\r\n");
        fwrite($cs, "NICK " . $config['nick'] . "\r\n");
        fwrite($cs, "USER " . $config['nick'] . "\r\n");
        fwrite($cs, "JOIN " . $config['channel'] . "\r\n");

        $lgnFile = fopen($lgnFileLoc, "a");
        fwrite($lgnFile, "IRC login succeeded at ".date("H:i:s Y-m-d").".\r\n");
        fclose($lgnFile);

        echo "IRC login succeeded.\r\n";

        $done = false;
        $tries = 0;

        while (!$done && $tries < 15) {
            $line = fgets($cs,128);
            $tries++;
            $done = strpos($line,">");
        }

        $done = false;
        while (!feof($cs) && !$done) {
            $line = fgets($cs,128);
            $done = strpos($line,"End of");
            flush();
        }

        while (!feof($cs)) {
            $line = fgets($cs,128);
            if (strlen($line) > 0) {
                $tokens = explode(' ',$line);

                if ($tokens[0] == 'PING') {
                    fwrite($cs,"PONG " . $tokens[1]);

                } else {
                    fwrite($cs, "PART ". $config['channel'] . "\r\n");
                    exit();
                    
                }
                flush();
            }
            sleep(1);
        }     
    } else {

        $lgnFile = fopen($lgnFileLoc, "a");
        fwrite($lgnFile, "IRC login failed at ".date("H:i:s Y-m-d").". Error ".$errno.": ".$errstr."\r\n");
        fclose($lgnFile);

        echo "IRC login failed.\r\n";

        exit();
    }
}

IRC($user, $ch, $pass);
}
?>