In a farewell visit to Iraq, President Bush on Sunday defended his handling of the war but warned that it was "not over" yet, nearly six years after he launched the invasion that toppled a brutal dictator but left Iraq, and the president`s legacy, struggling to recover, according to Los Angeles Times.

The unannounced visit, which lasted about eight hours, came 37 days before Bush hands power to President-elect Barack Obama, who is expected to oversee the departure of most if not all of the nearly 150,000 American troops in Iraq.

He later flew to Afghanistan, where he met with President Hamid Karzai and attended a rally of U.S. and allied soldiers at Bagram Air Base. He told the troops that they were making "hopeful gains" in a country where they are battling a growing insurgency. Thousands more U.S. troops are likely to be sent to Afghanistan next year to join about 30,000 already there.

In a final speech in Iraq to cheering U.S. forces in one of Saddam Hussein`s former palaces, Bush said his decision to bolster the American troop presence early last year to quell sectarian bloodshed was "one of the greatest successes in the history of the United States military."

"Thanks to you, the Iraq we`re standing in today is dramatically freer, dramatically safer and dramatically better than the Iraq we found eight years ago," he said before boarding Air Force One for the flight home.

But in a sign of the lingering animosity many Iraqis have toward Bush, and in a moment that undercut White House hopes of an enthusiastic, glitch-free visit to a relatively quiet nation, an Iraqi journalist hurled his shoes across the room at Bush and called him a "dog," the height of insults in the Arab world. The shoes slammed into the wall behind Bush and Prime Minister Nouri Maliki, who proceeded to take questions from other journalists after the assailant was wrestled to the ground and taken away.

The president, upon landing at Baghdad`s airport, was greeted by U.S. Ambassador Ryan Crocker and Army Gen. Ray Odierno, the commander of U.S. troops in Iraq. Bush then threw himself into a flurry of meetings with Iraqi officials, including President Jalal Talabani, Vice Presidents Adel Abdul Mehdi and Tariq Hashimi, and Maliki.

"The work hasn`t been easy, but it`s been necessary," Bush said after his meeting with Talabani, Abdul Mehdi and Hashimi.

Bush said his visit was in part "to herald the passage" of a Status of Forces Agreement for the withdrawal of all U.S. forces by the end of 2011 and an accompanying accord outlining future U.S.-Iraqi relations.

Bush called the package "a reminder of our friendship and as a way forward to help the Iraqi people realize the blessings of a free society." Some critics say it is not clear enough in its deadlines for U.S. troop pullouts, and other critics say it is likely to negate his promise that America would stay as long as it took for Iraq to remain a stable democracy.

The Bush visit, which like his three previous ones was kept secret until his arrival, reflected the changes that have been taking place in Iraq in the aftermath of what he has called his "surge" strategy. In a sign of improved security since his last trip here in September 2007, and perhaps to burnish his image, Bush`s distinctive jet landed in Baghdad in broad daylight, and he ventured beyond military bases and the heavily guarded Green Zone in central Baghdad.

His first stop was Talabani`s palace in the capital`s Karada district, outside the Green Zone, where he walked slowly up a long, red carpet with Talabani. After dark, when the meeting was over, Bush`s unmarked motorcade drove through the quiet, chilly streets and crossed the bright green bridge leading into the Green Zone for meetings with more Iraqi officials, including Maliki. The streets were empty, having been closed hours earlier to most traffic when security officials got word of Bush`s arrival.

Appearing alongside Maliki, Bush, who only recently acknowledged regrets about relying on bad intelligence about weapons of mass destruction when he invaded Iraq in March 2003, declared, "The war is not over."

The words were in stark contrast to those he uttered May 1, 2003, shortly after Hussein`s overthrow, when Bush announced that "major combat operations have ended" and that attention could turn to sustaining security and rebuilding the country.

Since then, 4,060 more U.S. forces have died in Iraq, according to the independent website icasualties.org, and tens of thousands of Iraqis have been killed. Bush`s popularity level is at a record low. Most Iraqis remain without reliable electricity, clean water, sewage systems and security, and pressure has mounted on the U.S.-backed Iraqi government to end the American presence here.

That pressure led Iraqi negotiators to demand a firm withdrawal date from the United States during months of talks over the Status of Forces Agreement, which was approved by Iraq`s parliament Nov. 27 and will govern the U.S. troop presence as of Jan. 1. The pact mandates that American combat troops leave cities by June 30 and that all U.S. forces be out of Iraq by the end of 2011.

Fierce debate, including a near-brawl in parliament, preceded the approval as opponents warned that it could be manipulated to extend the U.S. presence. Maliki got the votes he sought only after promising a national referendum in July that could force his government to cancel the pact if voters oppose it. But in a sign of confidence in the agreement, Bush and Maliki signed the document Sunday.

The Iraq war plays a key role in defining Bush`s legacy and is largely responsible for the plunge in his public support to historically low levels. Yet, as he took this final lap, Iraq has become a relative bright spot in his foreign policy record. Although military leaders emphasize that the situation in Iraq could still revert to mayhem, it has nonetheless grown more stable.

Daily attacks, which once numbered in the dozens, now average about four, U.S. officials say. Meanwhile, the administration`s efforts in Afghanistan, Pakistan, Iran and North Korea, along with Bush`s campaign to promote Arab-Israeli peace, are deteriorating or gridlocked.

The Baghdad visit comes in a month when the White House has been pressing a public campaign to advance its view that Bush is leaving behind a stronger record of accomplishment than widely thought. On Dec. 5, Bush delivered a speech in which he declared that he was leaving the Middle East a more hopeful place -- a judgment that many would dispute.

"His wrong acts eventually divided the people of Iraq into sects, political entities and blocs, and as a consequence we are unable to reestablish our state," said Usama Najafi, a lawmaker with the secular Iraqi National List coalition in parliament. Najafi blamed Bush for Iranian interference in Iraq, saying the U.S. presence had given Iran an excuse to send weapons to anti-U.S. militias.

"We cannot rebuild our country because of the fragile base which was formed on mistakes, and even President Bush is unable to convince the international community of the reasons behind his policy in Iraq," Najafi said.

Fawzi Akram, a lawmaker with an anti-U.S. Shiite Muslim bloc, said he wished Bush would "stand in the city center of Baghdad and apologize to all the people of Iraq."

As word of Bush`s visit spread, mosques in Baghdad`s Sadr City district, a stronghold of Shiite cleric Muqtada Sadr, began blaring their sirens and calling for people to march in protest today.

But Abbas Bayati, a lawmaker with the main Shiite bloc in parliament, praised Bush "for his bravery in getting rid of the previous regime" and said any errors in planning for the aftermath of Hussein`s regime should be viewed in context.

"True, there were many sacrifices that were made, plenty of pain and agony. Maybe the American administration was not very precise in planning what would come afterward," Bayati said. "But to start this project by removing Saddam and then to finish it off with the making of the [Status of Forces Agreement] is something great."