NEW YORK - The debate over the National Security Agency's collection of millions of Americans' telephone records fell squarely into the courts when a federal judge in Manhattan upheld the legality of the program, citing its need in the fight against terrorism, just days after another federal judge concluded it was likely unconstitutional.

The ruling Friday by U.S. District Judge William H. Pauley 3d and the opposing view earlier this month by U.S. District Judge Richard Leon in Washington set the stage for federal appeals courts to contemplate the delicate balance between individual rights set out in the Constitution and the need to protect national security.

Pauley concluded the program was a necessary extension of steps taken after the Sept. 11 terrorist attacks. He said the program lets the government connect fragmented and fleeting communications and "represents the government's counter-punch" to the al-Qaeda terrorist network's use of technology to operate decentralized and plot international terrorist attacks remotely.

"This blunt tool only works because it collects everything," Pauley said. "The collection is broad, but the scope of counterterrorism investigations is unprecedented."

Pauley's decision contrasts with Leon's grant of a preliminary injunction against the collection of phone records of two men who had challenged the program. The Washington jurist said the program likely violates the Constitution's ban on unreasonable search. The judge has since stayed the effect of his ruling, pending a government appeal.

Both cases now move to appeals courts and a conflict that some say will eventually be settled by the Supreme Court. The chances that the nation's top court will address the issue increase if the appeals courts reach differing opinions or if the current use of the program is declared illegal.

Pauley's ruling dismissed a lawsuit brought by the American Civil Liberties Union this year after former NSA analyst Edward Snowden leaked details of the secret programs that critics say violate privacy rights. The NSA-run programs pick up millions of telephone and Internet records that are routed through American networks each day.

Pauley said the mass collection of phone data "significantly increases the NSA's capability to detect the faintest patterns left behind by individuals affiliated with foreign terrorist organizations. Armed with all the metadata, NSA can draw connections it might otherwise never be able to find." He added: "As the Sept. 11 attacks demonstrate, the cost of missing such a threat can be horrific."

Pauley said the attacks "revealed, in the starkest terms, just how dangerous and interconnected the world is. While Americans depended on technology for the conveniences of modernity, al-Qaeda plotted in a seventh-century milieu to use that technology against us. It was a bold jujitsu. And it succeeded because conventional intelligence gathering could not detect diffuse filaments connecting al-Qaeda."

He wrote that the NSA intercepted seven calls made by one of the Sept. 11 hijackers in San Diego before the attacks, but mistakenly concluded that he was overseas because it lacked the kind of information it can now collect.

"The question for this court is whether the government's bulk telephone metadata program is lawful. This court finds it is. But the question of whether that program should be conducted is for the other two coordinate branches of government to decide," he said.

The ACLU promised to appeal to the U.S. Court of Appeals for the Second Circuit in Manhattan.