Italy beat Bulgaria 2-1 but their worst nightmare was realised when Denmark and Sweden drew 2-2 to knock the Azzurri out of Euro 2004.
Giovanni Trapattoni's men were made to suffer and got the win they needed but the other Group C result meant both Scandinavian countries advanced to the quarter-final stages on goal difference.
They dominated the early stages but it took them 14 minutes before they could test Bulgaria goalkeeper Zdravko Zdravkov.
Cassano's fine cross from the right side found an unmarked Stefano Fiore, whose acrobatic shot from close range was superbly parried by Zdravkov but only as far as Alessandro del Piero, whose left-footed attempt went narrowly wide of the goalkeeper's right-hand post.
Five minutes later, Bernardo Corradi should have given his side the lead. Gianluca Zambrotta's perfect cross into the box was flicked on by Cassano but the unmarked Corradi headed straight at Zdravkov.
In the 29th minute, Martin Petrov finally tested goalkeeper Gianluigi Buffon with a powerful left-footed effort from the edge of the area before Cassano fired one wide at the other end.
One minute before the break, disaster struck for the Azzurri when Russian referee Valentin Ivanov pointed to the spot after defender Marco Materazzi held on to Petrov inside the area.
It was Petrov whose precise shot towards the left post beat Buffon to give his side a 1-0 lead, and Bulgaria's first goal of the tournament.
Bulgaria's celebrations did not last long, however, as Italy's prayers were answered three minutes after the restart.
Zdravkov spilled the ball after Cassano had struck the crossbar and Perrotta pounced to score. The goal brought Italy alive and they made their first substitution on 53 minutes, with Vieri replacing Corradi.
The Inter Milan forward could have given his team the lead shortly after, but his forced header following Andrea Pirlo's precise cross went off-target.
The Azzurri still required one more goal if they aimed to reach the last eight at this stage and del Piero should have made it 2-1 in the 68th minute but his angled shot from the box went straight to Zdravkov's hands.
With 12 minutes remaining Zambrotta tried his luck from distance but his shot on target was deflected wide by a defender. Seconds later, Trapattoni played his last card with Juventus forward Marco di Vaio replacing Materazzi.
Dimitar Berbatov could then have finished off Italy, had Buffon not intervened. The Juventus goalkeeper made a superb save following the Bayer Leverkusen striker's free-kick from the edge of the area.
One minute before the end, Italy took the lead with Cassano's close-range shot finishing in the net but the players' celebrations soon ended with Trapattoni confirming their worst nightmare had taken place in Porto.
Filed by Barry McEneaney