public function set_flag($uids, $flag, $folder=null, $skip_cache=false)
    {
        if (!strlen($folder)) {
            $folder = $this->folder;
        }

        if (!$this->check_connection()) {
            return false;
        }

        $flag = strtoupper($flag);
        list($uids, $all_mode) = $this->parse_uids($uids);

        if (strpos($flag, 'UN') === 0) {
            $result = $this->conn->unflag($folder, $uids, substr($flag, 2));
        }
        else {
            $result = $this->conn->flag($folder, $uids, $flag);
        }

        if ($result && !$skip_cache) {
            // reload message headers if cached
            // update flags instead removing from cache
            if ($mcache = $this->get_mcache_engine()) {
                $status = strpos($flag, 'UN') !== 0;
                $mflag  = preg_replace('/^UN/', '', $flag);
                $mcache->change_flag($folder, $all_mode ? null : explode(',', $uids),
                    $mflag, $status);
            }

            // clear cached counters
            if ($flag == 'SEEN' || $flag == 'UNSEEN') {
                $this->clear_messagecount($folder, array('SEEN', 'UNSEEN'));
            }
            else if ($flag == 'DELETED' || $flag == 'UNDELETED') {
                $this->clear_messagecount($folder, array('ALL', 'THREADS'));
                if ($this->options['skip_deleted']) {
                    // remove cached messages
                    $this->clear_message_cache($folder, $all_mode ? null : explode(',', $uids));
                }
            }

            $this->set_search_dirty($folder);
        }

        return $result;
    }