diff --git a/varnish.admin.inc b/varnish.admin.inc
index 869af76..5f6615a 100644
--- a/varnish.admin.inc
+++ b/varnish.admin.inc
@@ -31,11 +31,16 @@ function varnish_admin_settings_form() {
     '#description' => t('Internally Drupal will attempt to flush its page cache every time cron.php runs. This can mean too-frequent cache flushes if you have cron running frequently. NOTE: this cache flush is global!'),
   );

-  $form['varnish_legacy'] = array(
-    '#type' => 'checkbox',
-    '#title' => t('Varnish Legacy Mode'),
-    '#default_value' => variable_get('varnish_legacy', 0),
-    '#description' => t('Check this if you use a version of varnish lower than 2.1'),
+  $form['varnish_version'] = array(
+    '#type' => 'select',
+    '#title' => t('Varnish version'),
+    '#default_value' => variable_get('varnish_version', 2.1),
+    '#description' => t('Select your varnish version.'),
+    '#options' => array(
+      '2' => '2.0.x',
+      '2.1' => '2.1.x',
+      '3' => '3.x',
+    ),
   );

   $form['varnish_control_terminal'] = array(
@@ -83,7 +88,7 @@ function varnish_admin_settings_form() {
   $form['varnish_stats'] = array(
     '#type' => 'item',
     '#title' => t('Status'),
-    '#markup' => theme('varnish_status', varnish_get_status()),
+    '#markup' => theme('varnish_status', varnish_get_status(), floatval(variable_get('varnish_version', 2.1))),
   );

   return system_settings_form($form);
@@ -103,6 +108,9 @@ function varnish_admin_settings_form_validate($form, &$form_state) {
  * Menu callback for varnish admin settings.
  */
 function varnish_admin_reports_page() {
+  if (floatval(variable_get('varnish_version', 2.1)) >= 3) {
+    return t('Varnish 3 has removed the option to display statistics directly from the terminal. Use varnishstat instead.');
+  }
   // Connect to varnish and do a full status report.
   $status = _varnish_terminal_run(array('stats'));
   $output = '';
diff --git a/varnish.install b/varnish.install
new file mode 100644
index 0000000..6c26dde
--- /dev/null
+++ b/varnish.install
@@ -0,0 +1,18 @@
+<?php
+
+/**
+ * @file
+ * Updates for the varnish module.
+ */
+
+/**
+ * Changes the varnish_legacy variable to a varnish_version variable
+ * that is a little more flexible.
+ */
+function varnish_update_7100(&$sandbox) {
+  $legacy = variable_get('varnish_legacy', 0);
+  if ($legacy) {
+    variable_set('varnish_version', 2.0);
+  }
+  return array();
+}
diff --git a/varnish.module b/varnish.module
index 59032f0..b0ffea6 100644
--- a/varnish.module
+++ b/varnish.module
@@ -36,13 +36,18 @@ function varnish_menu() {
     'type' => MENU_DEFAULT_LOCAL_TASK,
     'weight' => -5,
   );
-  $items['admin/reports/varnish'] = array(
-    'title' => 'Varnish status',
-    'description' => 'Get the output of varnishstat.',
-    'page callback' => 'varnish_admin_reports_page',
-    'access arguments' => array('administer varnish'),
-    'file' => 'varnish.admin.inc',
-  );
+  // Varnish 3 has removed the stats command from the terminal, so
+  // we can't provide this functionality for varnish 3 users
+  // currently.
+  if (floatval(variable_get('varnish_version', 2.1)) < 3) {
+    $items['admin/reports/varnish'] = array(
+      'title' => 'Varnish status',
+      'description' => 'Get the output of varnishstat.',
+      'page callback' => 'varnish_admin_reports_page',
+      'access arguments' => array('administer varnish'),
+      'file' => 'varnish.admin.inc',
+    );
+  }
   return $items;
 }

@@ -52,7 +57,7 @@ function varnish_menu() {
 function varnish_theme() {
   return array(
     'varnish_status' => array(
-      'arguments' => array('status' => array()),
+      'arguments' => array('status' => array(), 'version' => 2.1),
     )
   );
 }
@@ -110,7 +115,8 @@ function varnish_expire_cache($paths) {
   $host = _varnish_get_host();
   $base = base_path();
   $purge = implode('$|^' . $base, $paths);
-  _varnish_terminal_run(array('purge req.http.host ~ ' . $host . ' && req.url ~ "^' . $base . $purge . '$"'));
+  $purge = '^'. $base . $purge .'$';
+  varnish_purge($host, $purge);
 }

 /**
@@ -119,7 +125,20 @@ function varnish_expire_cache($paths) {
 function varnish_purge_all_pages() {
   $path = base_path();
   $host = _varnish_get_host();
-  _varnish_terminal_run("purge req.http.host ~ $host && req.url ~ ^$path");
+  varnish_purge($host, $path);
+}
+
+/**
+ * Helper function to purge items for a host that matches the provided pattern.
+ * @param string $host the host to purge.
+ * @param string $pattern the pattern to look for and purge.
+ */
+function varnish_purge($host, $pattern) {
+  // Get the current varnish version, if we are using Varnish 3.x, then we can
+  // need to use ban instead of purge.
+  $version = floatval(variable_get('varnish_version', 2.1));
+  $command = $version >= 3 ? "ban" : "purge";
+  _varnish_terminal_run(array("$command req.http.host ~ $host && req.url ~ \"$pattern\""));
 }

 /**
@@ -155,7 +174,13 @@ function theme_varnish_status($status) {
     list($server, $port) = explode(':', $terminal);
     if ($state == VARNISH_SERVER_STATUS_UP) {
       $icon = theme('image', array('path' => 'misc/watchdog-ok.png', 'alt' => "Server OK: {$server}:{$port}", "{$server}:{$port}"));
-      $items[] = t('!status_icon Varnish running. Observe more detailed statistics !link.', array('!status_icon' => $icon, '!link' => l(t('here'), 'admin/reports/varnish')));
+      $version = floatval(variable_get('varnish_version', 2.1));
+      if ($version < 3) {
+        $items[] = t('!status_icon Varnish running. Observe more detailed statistics !link.',
+                array('!status_icon' => $icon, '!link' => l(t('here'), 'admin/reports/varnish')));
+      }
+      else {
+        $items[] = t('!status_icon Varnish running.', array('!status_icon' => $icon));
+      }
     }
     else {
       $icon = theme('image', array('path' => 'misc/watchdog-error.png', 'alt' => 'Server down: {$server}:{$port}', "{$server}:{$port}"));
@@ -209,7 +234,7 @@ function _varnish_terminal_run($commands) {
       continue;
     }
     // If there is a CLI banner message (varnish >= 2.1.x), try to read it and move on.
-    if (variable_get('varnish_legacy', 0) != 1) {
+    if(floatval(variable_get('varnish_version', 2.1)) > 2.0) {
       $status = _varnish_read_socket($client);
       // Do we need to authenticate?
       if ($status['code'] == 107) { // Require authentication
diff --git a/varnish.test b/varnish.test
index 574fcc1..09d633f 100644
--- a/varnish.test
+++ b/varnish.test
@@ -64,7 +64,9 @@ class VarnishConnectionTestCase extends VarnishTestCase {
   }

   function testVarnishStats() {
-    $this->assertVarnishCommand(array('stats'));
+    if (variable_get('varnish_version', 2.1) < 3) {
+      $this->assertVarnishCommand(array('stats'));
+    }
   }

   function testMultipleCommands() {
@@ -166,7 +168,7 @@ class VarnishAdminTestCase extends VarnishTestCase {
     return array(
       'varnish_control_terminal' => variable_get('varnish_control_terminal', '127.0.0.1:6082'),
       'varnish_flush_cron' => variable_get('varnish_flush_cron', 0),
-      'varnish_legacy' => variable_get('varnish_legacy', 0),
+      'varnish_version' => variable_get('varnish_version', 2.1),
       'varnish_control_key' => variable_get('varnish_control_key', 0),
       'varnish_socket_timeout' => variable_get('varnish_socket_timeout', 100),
       'varnish_cache_clear' => variable_get('varnish_cache_clear', 0),
