President Bush and Pakistani President Pervez Musharraf recommitted their nations Saturday to the difficult task of hunting down terrorists still hiding here and across the globe. Before departing for home, Bush praised Musharraf as a "man of courage and vision."

Bush made his first visit to Pakistan — despite terrorist dangers that demanded extraordinary security — to bolster Musharraf, who straddles a delicate political divide in this impoverished but growing Islamic nation.

The U.S.-led wars in Iraq and Afghanistan are unpopular here, and Pakistan's strong anti-American sentiment was reflected in the thousands who demonstrated across the country against Bush's visit. While there are suspicions that Al Qaeda and Taliban operatives maintain some degree of safe sanctuary inside Pakistan, Musharraf has defied criticism he is too cozy with Washington to be a strong U.S. partner in the anti-terrorism campaign.

Bush said the visit convinced him that Musharraf is as committed as ever.

"We will win this fight together," Bush said after private talks with Musharraf. "While we do have a lot of work to be done, it's important that we stay on the hunt."

The United States also sees Musharraf as a leader who favors a more open, moderate and tolerant Pakistan. Standing alongside the Pakistani leader whom Bush calls his "buddy," the U.S. president stopped short of criticizing Musharraf on the pace of democratic advances, only gently calling for elections scheduled for next year to be "open and honest."

Musharraf seized power in a 1999 bloodless coup. Instead of giving up his military uniform in 2004 as promised, he changed the constitution so he could hold both his army post and the presidency until 2007.

"I believe democracy is Pakistan's future," Bush said at an outdoor news conference with Musharraf at the marble presidential palace.

Musharraf defended his record on democracy, touting steps to liberalize Pakistan's press, usher in an elected parliament and empower women.

"Beyond 2007, this is an issue that has to be addressed and according to the constitution of Pakistan, and I will never violate the constitution," said the Pakistani leader, repeating similar reassurances made in the past. "Democracy will prevail."

The Pakistani government once supported the repressive Taliban regime in Afghanistan. But after the 2001 terrorists attacks on America, Musharraf aligned himself with Bush and the war on terrorism. Musharraf has been the target of repeated assassination attempts as a result.

Pakistan's law enforcement agencies have arrested more than 700 suspected militants in the past four years.

"The intentions of Pakistan and my intentions are absolutely clear — that we have a strong partnership on the issue of fighting terrorism," Musharraf said.

The day before the president arrived, an American diplomat was killed in a suicide car-bombing at a U.S. consulate in the southern city of Karachi, a hotbed of Islamic militancy. Musharraf called it a vicious act timed to coincide with Bush's visit.

On Saturday, Pakistani police detained Imran Khan and arrested dozens of his opposition party's supporters to thwart a planned protest. Khan, a respected former Pakistani cricket captain, has condemned Musharraf as an "American slave." Police beat and arrested at least 16 people chanting slogans against the two presidents.

In Karachi, about 500 people gathered at small rallies, chanting, "Killer of Afghans — George Bush!" and "Killer of Iraq — George Bush!" Smaller groups of protesters rallied in the eastern city of Multan, as well as in Lahore.

Hoping to broadcast American compassion to the Muslim world, Bush showcased the U.S. assistance offered after an earthquake devastated Pakistan in October, killing 86,000 people and leaving more than 2 million homeless.

"It is staggering what the people of this country have been through," said Bush, who earlier saw a film on the disaster and visited with victims, including orphans, widows, women in wheelchairs and children who lost limbs. "We're proud to help."

Musharraf said Pakistan would have been hard-pressed to handle relief operations without U.S. military Chinook helicopters and medical assistance.

Bush also sought to endear himself to Pakistanis by playing a game of cricket, the national pastime, with about two dozen children on the grounds of the U.S. Embassy.

At a state dinner before departing for Washington, Bush saluted Pakistan's president.

"Pakistan has a bright future because of its proud people and because of the work of a strong leader," he said just before a dinner that was unusual because there were no tuxedos and the heads of state and their wives sat at a very large, round table with other guests.

To Musharraf, Bush said: "You've proved yourself to be a man of courage and vision. I'm honored to be your partner."

American and green-and-white Pakistani flags were hoisted in honor of Bush's visit. Streets in the capital were mostly empty, except for armed security officers standing guard.

Layers of security, including three helicopters that circled overhead, shadowed Bush's motorcade Saturday as it ferried him from the fortified U.S. Embassy compound to the presidential palace in the heart of the city's government district. Bush was escorted down a red carpet behind raised swords gripped by Pakistani troops in dark green uniforms.

The visit followed a three-day trip to India, where Bush sealed a civilian nuclear deal. Pakistan has asked for the same arrangement, but Bush made clear that was unlikely, using diplomatic language about the two countries' "different needs and different histories."

Just two years ago Pakistan's leading nuclear scientist, A.Q. Khan, was exposed as the chief of a lucrative black market in weapons technology that had supplied Iran, Libya and North Korea. Pakistan's government denied any knowledge of his proliferation activities.

But Bush said the United States was committed to helping Pakistan meet its rising energy needs. He expressed no objections to plans by India and Pakistan to build a pipeline to bring much-needed natural gas supplies from Iran, a project that had brought U.S. disapproval. Washington opposes investments that benefit Iran, which it suspects of trying to build nuclear weapons.