"Britain's commitment to Gibraltar is absolute, it's unshakable and it will endure whatever the decision in the referendum," Foreign Secretary Philip Hammond said referring to the June 23 vote on Britain's membership of the European Union.
"But I have to say this. Britain's ability to protect Gibraltar's interest will be seriously impaired if we are no longer members of the European Union, if we are no longer sitting around the table in Brussels when the decisions are made," he said.
"We need to recognize that, with the best will in the world, Britain will not able to solve all the challenges that Gibraltar could face if there was an exit on June 23rd."
Spain ceded Gibraltar to Britain in perpetuity in 1713 but has long argued that it should be returned to Spanish sovereignty.
The possibility of Britain leaving the EU has raised fears in Gibraltar, where many of the tiny territory's 33,000 residents worry that their only land access to the rest of Europe would be affected.
The small land border between Gibraltar and Spain has long been a source of tension.
Spanish dictator Francisco Franco went as far as closing the crossing in 1969, all but stranding inhabitants who had to rely on air and boat links until it was fully re-opened in 1985, a decade after the general died as
Madrid pushed a bid to join the EU.
Spain's conservative government in July 2013 imposed stringent controls at its border with Gibraltar, leading to lengthy queues for motorists, in what it said was a move aimed at clamping down on cigarette smuggling.
But Gibraltar said the enhanced border controls were in retaliation for the installation of an artificial reef in its waters that had prevented Spanish boats from fishing there.
The European Commission, the bloc's executive arm, sent inspectors to the British territory in September after Britain and Gibraltar complained to Brussels.
With Britain out of the EU, Gibraltar's border would become an external and not an internal EU frontier which has to be kept open under EU rules.
Spain could close the border and a legal challenge by Britain and Gibraltar would be more difficult.
While polls show the "Remain" and "Leave" campaigns are running neck-and-neck in mainland Britain, the vast majority of Gibraltar's roughly 33,000 residents want Britain to remain in the European Union.
A poll published last month by the Gibraltar Chronicle, the local newspaper, found that 88 per cent of the population would back EU membership in the referendum on June 23rd. Turnout is expected to be above 80 per cent.
Spain's foreign ministry issued a statement during Hammond's visit saying that Gibraltar is on a United Nations list of non-self-governing territories that are subject to a process of decolonization, calling the territory a "historical anachronism".
"This colonial vestige destroys Spain's national unity and territorial integrity," the statement said.
Spain's acting foreign minister told Spanish radio in March that if Britain left the EU he would "raise the question of Gibraltar" the very next day.
Gibraltar reacted angrily to the remark.
"The declared intent of the Caretaker Foreign Minister of Spain to bring the question of Gibraltar to the fore in the event of the UK and Gibraltar leaving the EU confirms the analysis that has already been made,” the Government of Gibraltar said in a statement to The Local made at the time.